zoukankan      html  css  js  c++  java
  • ftp文件操作

    转载:

    https://blog.csdn.net/zhangxiaomin1992/article/details/73644279?tdsourcetag=s_pcqq_aiomsg


    /**

    * ftp上传文件

    *

    * @param url

    * FTP服务器hostname

    * @param port

    * FTP服务器端口

    * @param username

    * FTP登录账号

    * @param password

    * FTP登录密码

    * @param path

    * FTP服务器保存目录

    * @param filename

    * 上传到FTP服务器上的文件名

    * @param input

    * 输入流

    * @return 成功返回true,否则返回false

    * @author 作者 wanghaixing

    * @date 创建时间:2017年6月13日 下午4:01:51

    * @version 1.0

    */

    public static boolean uploadFile(String url, int port, String username, String password, String path,

    String filename, InputStream input) {

    boolean success = false;

    FTPClient ftp = new FTPClient();

    try {

    int reply;

    ftp.connect(url, port);// 连接FTP服务器

    // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

    ftp.login(username, password);// 登录

    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

    reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {

    ftp.disconnect();

    return success;

    }

    ftp.changeWorkingDirectory(path);

    //ftp.storeFile(filename, input);

    ftp.appendFile(filename, input);

    input.close();

    ftp.logout();

    success = true;

    } catch (IOException e) {

    e.printStackTrace();

    } finally {

    if (ftp.isConnected()) {

    try {

    ftp.disconnect();

    } catch (IOException ioe) {

    }

    }

    }

    return success;

    }

    /**

    * 从ftp下载文件

    * @param url

    * FTP服务器hostname

    * @param port

    * FTP服务器端口

    * @param username

    * FTP登录账号

    * @param password

    * FTP登录密码

    * @param remotePath

    * FTP服务器上的相对路径

    * @param fileName

    * 要下载的文件名

    * @param localPath

    * 下载后保存到本地的路径

    * @return

    */

    public static boolean downFile(String url, int port, String username, String password, String remotePath,

    String fileName, String localPath) {

    boolean success = false;

    boolean isFileExits = false;

    FTPClient ftp = new FTPClient();

    try {

    int reply;

    ftp.connect(url, port);

    // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

    ftp.login(username, password);// 登录

    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

    reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {

    ftp.disconnect();

    return success;

    }

    ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录

    FTPFile[] fs = ftp.listFiles();

    for (FTPFile ff : fs) {

    if (ff.getName().equals(fileName)) {

    isFileExits=true;

    File localFile = new File(localPath + "/" + ff.getName());

    OutputStream is = new FileOutputStream(localFile);

    ftp.retrieveFile(ff.getName(), is);

    is.close();

    }

    }

    if(!isFileExits) {

    throw new RuntimeException("要下载的文件不存在!");

    }

    ftp.logout();

    success = true;

    } catch (IOException e) {

    e.printStackTrace();

    } finally {

    if (ftp.isConnected()) {

    try {

    ftp.disconnect();

    } catch (IOException ioe) {

    }

    }

    }

    return success;

    }

    /**

    * 查询ftp服务器上指定路径所有文件名

    *

    * @param url

    * FTP服务器hostname

    * @param port

    * FTP服务器端口

    * @param username

    * FTP登录账号

    * @param password

    * FTP登录密码

    * @param remotePath

    * FTP服务器上的相对路径

    * @return

    * @date 创建时间:2017年6月14日 下午5:06:57

    */

    public static List<String> listFTPFiles(String url, int port, String username, String password, String remotePath) {

    ArrayList<String> resultList = new ArrayList<String>();

    FTPClient ftp = new FTPClient();

    try {

    int reply;

    ftp.connect(url, port);

    // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

    ftp.login(username, password);// 登录

    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

    reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {

    ftp.disconnect();

    return resultList;

    }

    ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录

    FTPFile[] fs = ftp.listFiles();

    for (FTPFile ff : fs) {

    resultList.add(ff.getName());

    // if (ff.getName().equals(fileName)) {

    // File localFile = new File(localPath + "/" + ff.getName());

    // OutputStream is = new FileOutputStream(localFile);

    // ftp.retrieveFile(ff.getName(), is);

    // is.close();

    // }

    }

    ftp.logout();

    } catch (IOException e) {

    e.printStackTrace();

    } finally {

    if (ftp.isConnected()) {

    try {

    ftp.disconnect();

    } catch (IOException ioe) {

    }

    }

    }

    return resultList;

    }

    /**

    * 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建

    * @param url

    * @param port

    * @param username

    * @param password

    * @param remote

    * @return

    * @throws IOException

    * @date 创建时间:2017年6月22日 上午11:51:33

    */

    public static boolean CreateDirecroty(String url, int port, String username, String password, String remote)

    throws IOException {

    FTPClient ftp = new FTPClient();

    int reply;

    ftp.connect(url, port);

    // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器

    ftp.login(username, password);// 登录

    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

    reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {

    ftp.disconnect();

    }

    ftp.changeWorkingDirectory("/var/www/html");// 转移到FTP服务器目录

    boolean success = true;

    String directory = remote + "/";

    // String directory = remote.substring(0, remote.lastIndexOf("/") + 1);

    // 如果远程目录不存在,则递归创建远程服务器目录

    if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory), ftp)) {

    int start = 0;

    int end = 0;

    if (directory.startsWith("/")) {

    start = 1;

    } else {

    start = 0;

    }

    end = directory.indexOf("/", start);

    String path = "";

    String paths = "";

    while (true) {

    String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");

    path = path + "/" + subDirectory;

    if (!existFile(path, ftp)) {

    if (makeDirectory(subDirectory, ftp)) {

    changeWorkingDirectory(subDirectory, ftp);

    } else {

    System.out.println("创建目录[" + subDirectory + "]失败");

    changeWorkingDirectory(subDirectory, ftp);

    }

    } else {

    changeWorkingDirectory(subDirectory, ftp);

    }

    paths = paths + "/" + subDirectory;

    start = end + 1;

    end = directory.indexOf("/", start);

    // 检查所有目录是否创建完毕

    if (end <= start) {

    break;

    }

    }

    }

    return success;

    }

    /**

    * 改变目录路径

    * @param directory

    * @param ftp

    * @return

    * @date 创建时间:2017年6月22日 上午11:52:13

    */

    public static boolean changeWorkingDirectory(String directory, FTPClient ftp) {

    boolean flag = true;

    try {

    flag = ftp.changeWorkingDirectory(directory);

    if (flag) {

    System.out.println("进入文件夹" + directory + " 成功!");

    } else {

    System.out.println("进入文件夹" + directory + " 失败!");

    }

    } catch (IOException ioe) {

    ioe.printStackTrace();

    }

    return flag;

    }

    /**

    * 创建目录

    * @param dir

    * @param ftp

    * @return

    * @date 创建时间:2017年6月22日 上午11:52:40

    */

    public static boolean makeDirectory(String dir, FTPClient ftp) {

    boolean flag = true;

    try {

    flag = ftp.makeDirectory(dir);

    if (flag) {

    System.out.println("创建文件夹" + dir + " 成功!");

    } else {

    System.out.println("创建文件夹" + dir + " 失败!");

    }

    } catch (Exception e) {

    e.printStackTrace();

    }

    return flag;

    }

    /**

    * 判断ftp服务器文件是否存在

    * @param path

    * @param ftp

    * @return

    * @throws IOException

    * @date 创建时间:2017年6月22日 上午11:52:52

    */

    public static boolean existFile(String path, FTPClient ftp) throws IOException {

    boolean flag = false;

    FTPFile[] ftpFileArr = ftp.listFiles(path);

    if (ftpFileArr.length > 0) {

    flag = true;

    }

    return flag;

    }

  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/ConfidentLiu/p/9683160.html
Copyright © 2011-2022 走看看