• FTP上传和下载工具类


    一、上传

     1 /**
     2      * ftp上传公开方法
     3      * @param host FTP服务器hostname
     4      * @param port FTP服务器端口
     5      * @param username FTP登录账号
     6      * @param password FTP登录密码
     7      * @param ftpPath FTP服务器文件存放路径。
     8      * @param filename 上传到FTP服务器上的文件名
     9      * @param input 本地要上传的文件的 输入流
    10      * @return
    11      */
    12     public static boolean doFtpUploadFile(String host, int port, String username, String password, String ftpPath, String filename, InputStream input){
    13         return uploadFile(host, port, username, password, ftpPath, filename, input);
    14     }
    15 
    16 
    17  /**
    18      * Description: 向FTP服务器上传文件
    19      * @param host FTP服务器hostname
    20      * @param port FTP服务器端口
    21      * @param username FTP登录账号
    22      * @param password FTP登录密码
    23      * @param ftpPath FTP服务器文件存放路径。
    24      * @param filename 上传到FTP服务器上的文件名
    25      * @param input 本地要上传的文件的 输入流
    26      * @return 成功返回true,否则返回false
    27      */  
    28     private static boolean uploadFile(String host, int port, String username, String password, String ftpPath, String filename, InputStream input) {
    29         
    30         boolean result = false;
    31         FTPClient ftp = new FTPClient();
    32         try {
    33             int reply;
    34             ftp.connect(host, port);// 连接FTP服务器
    35             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
    36             ftp.login(username, password);// 登录
    37             reply = ftp.getReplyCode();
    38             if (!FTPReply.isPositiveCompletion(reply)) {
    39                 ftp.disconnect();
    40                 return result;
    41             }
    42             //切换到上传目录
    43             if (!ftp.changeWorkingDirectory(ftpPath)) {
    44                 //如果目录不存在创建目录
    45                 String[] dirs = ftpPath.split("/");
    46                 String tempPath = "";
    47                 for (String dir : dirs) {
    48                     if (null == dir || "".equals(dir)) continue;
    49                     tempPath += "/" + dir;
    50                     if (!ftp.changeWorkingDirectory(tempPath)) {
    51                         if (!ftp.makeDirectory(tempPath)) {
    52                             return result;
    53                         } else {
    54                             ftp.changeWorkingDirectory(tempPath);
    55                         }
    56                     }
    57                 }
    58             }
    59             //设置上传文件的类型为二进制类型
    60             ftp.setFileType(FTP.BINARY_FILE_TYPE);
    61                      //防止端口阻塞
    62             ftp.enterLocalPassiveMode();
    63             //上传文件
    64             if (!ftp.storeFile(filename, input)) {
    65                 return result;
    66             }
    67             result = true;
    68         } catch (IOException e) {
    69             e.printStackTrace();
    70         } finally {
    71             try {
    72                 input.close();
    73                 ftp.logout();
    74                 if (ftp.isConnected()) {
    75                     ftp.disconnect();
    76                 }
    77             } catch (IOException ioe) {
    78                 ioe.printStackTrace();
    79             }
    80             
    81         }
    82         return result;
    83     }

    二、下载

     1  /**
     2      * 下载公开方法
     3      * @param host
     4      * @param port
     5      * @param username
     6      * @param password
     7      * @param remotePath
     8      * @param fileName
     9      * @param localPath
    10      * @return
    11      */
    12     public static boolean doFtpdownloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath){
    13         return downloadFile(host, port, username, password, remotePath, fileName, localPath);
    14     }
    15 
    16 
    17  /**
    18      * Description: 从FTP服务器下载文件
    19      * @param host FTP服务器hostname
    20      * @param port FTP服务器端口
    21      * @param username FTP登录账号
    22      * @param password FTP登录密码
    23      * @param remotePath FTP服务器上的相对路径
    24      * @param fileName 要下载的文件名
    25      * @param localPath 下载后保存到本地的路径
    26      * @return
    27      */  
    28     private static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) {
    29         
    30         boolean result = false;
    31         FTPClient ftp = new FTPClient();
    32         try {
    33             int reply;
    34             ftp.connect(host, port);
    35             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
    36             ftp.login(username, password);// 登录
    37             reply = ftp.getReplyCode();
    38             if (!FTPReply.isPositiveCompletion(reply)) {
    39                 ftp.disconnect();
    40                 return result;
    41             }
    42             ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
    43             FTPFile[] fs = ftp.listFiles();
    44             for (FTPFile ff : fs) {
    45                 if (ff.getName().equals(fileName)) {
    46                     File localFile = new File(localPath + "/" + ff.getName());
    47 
    48 
    49                     OutputStream is = new FileOutputStream(localFile);
    50                     ftp.retrieveFile(ff.getName(), is);
    51                     is.close();
    52                 }
    53             }
    54 
    55 
    56             result = true;
    57         } catch (IOException e) {
    58             e.printStackTrace();
    59         } finally {
    60             try {
    61                 ftp.logout();
    62                 if (ftp.isConnected()) {
    63                     ftp.disconnect();
    64                 }
    65             } catch (IOException ioe) {
    66                 ioe.printStackTrace();
    67             }
    68         }
    69         return result;
    70     }

     三、jar包下载

    https://files-cdn.cnblogs.com/files/lhq1996/commons-net-3.6.zip

  • 相关阅读:
    Linux 下用 Python 连接 MSSql Server 2008
    这周我加星(4)
    几个受益终身的英文缩写
    这周我加星(1)
    证据
    这周我加星(3)
    代友招人────3D 客户端程序员
    人在广州,力撑陈 Sir
    谁不会成为 Linux(ubuntu)的用户
    这周我加星(5)
  • 原文地址:https://www.cnblogs.com/lhq1996/p/13891508.html
Copyright © 2020-2023  润新知