ftp文件上传和下载

  1. 需要导入commons-net jar包
  2. <!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>3.6</version>
    </dependency>
    /**
     * 保存图片
     * @param pathRoot 容器路径
     * @param path 需要保存的路径
     * @param jumpPath 跳转的路径
     * @param describe 描述
     * @param displayOrder 排序
     * @param image 图片
     */

  3. /**  
  4.      * Description: 向FTP服务器上传文件  
  5.      * @param host FTP服务器hostname  
  6.      * @param port FTP服务器端口  
  7.      * @param username FTP登录账号  
  8.      * @param password FTP登录密码  
  9.      * @param basePath FTP服务器基础目录 
  10.      * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath 
  11.      * @param filename 上传到FTP服务器上的文件名  
  12.      * @param input 输入流  
  13.      * @return 成功返回true,否则返回false 
  14.      */    
  15. // (MultipartFile image)
  16. //String fileName = image.getOriginalFilename();
  17. //InputStream is = image.getInputStream();
  18. //uploadFile("111.11.11.11",21,"xxx","xxx111","image","",fileName,is);
  19.     public static boolean uploadFile(String host, int port, String username, 
  20. String password, String basePath,  String filePath, String filename, InputStream input) {  
  21.         boolean result = false;  
  22.         FTPClient ftp = new FTPClient();  
  23.         try {  
  24.             int reply;  
  25.             ftp.connect(host, port);// 连接FTP服务器  
  26.             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器  
  27.             ftp.login(username, password);// 登录  
  28.     /* ftp.enterLocalPassiveMode();*/
  29.     ftp.setBufferSize(1024);
  30.     ftp.setControlEncoding("gbk");
  31.     ftp.setFileType(FTP.BINARY_FILE_TYPE);
  32.     ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
  33.             reply = ftp.getReplyCode();  
  34.             if (!FTPReply.isPositiveCompletion(reply)) {  
  35.                 ftp.disconnect();  
  36.                 return result;  
  37.             }  
  38.             //切换到上传目录  
  39.             if (!ftp.changeWorkingDirectory(basePath+filePath)) {  
  40.                 //如果目录不存在创建目录  
  41.                 String[] dirs = filePath.split("/");  
  42.                 String tempPath = basePath;  
  43.                 for (String dir : dirs) {  
  44.                     if (null == dir || "".equals(dir)) continue;  
  45.                     tempPath += "/" + dir;  
  46.                     if (!ftp.changeWorkingDirectory(tempPath)) {  
  47.                         if (!ftp.makeDirectory(tempPath)) {  
  48.                             return result;  
  49.                         } else {  
  50.                             ftp.changeWorkingDirectory(tempPath);  
  51.                         }  
  52.                     }  
  53.                 }  
  54.             }  
  55.             //设置上传文件的类型为二进制类型  
  56.             ftp.setFileType(FTP.BINARY_FILE_TYPE);  
  57.             //上传文件  
  58.             if (!ftp.storeFile(newString(filename.getBytes("utf-8"),"iso-8859-1"), input)) {  
  59.                 return result;  
  60.             }  
  61.             input.close();  
  62.             ftp.logout();  
  63.             result = true;  
  64.         } catch (IOException e) {  
  65.             e.printStackTrace();  
  66.         } finally {  
  67.             if (ftp.isConnected()) {  
  68.                 try {  
  69.                     ftp.disconnect();  
  70.                 } catch (IOException ioe) {  
  71.                 }  
  72.             }  
  73.         }  
  74.         return result;  
  75.     }  
  76.       
  77.     /**  
  78.      * Description: 从FTP服务器下载文件  
  79.      * @param host FTP服务器hostname  
  80.      * @param port FTP服务器端口  
  81.      * @param username FTP登录账号  
  82.      * @param password FTP登录密码  
  83.      * @param remotePath FTP服务器上的相对路径  
  84.      * @param fileName 要下载的文件名  
  85.      * @param localPath 下载后保存到本地的路径  
  86.      * @return  
  87.      */    
  88. //(MultipartFile image)
  89. //String fileName = image.getOriginalFilename();
  90. //InputStream is = image.getInputStream();  
  91. //downloadFile("123.56.20.45",21,"feizhu","feizhu123","image",fileName,pathRoot+path);
  92.     public static boolean downloadFile(String host, int port, String username, 
  93. String password, String remotePath, String fileName, String localPath) {  
  94.         boolean result = false;  
  95.         FTPClient ftp = new FTPClient();  
  96.         try {  
  97.             int reply;  
  98.             ftp.connect(host, port);  
  99.             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器  
  100.             ftp.login(username, password);// 登录  
  101.             reply = ftp.getReplyCode();  
  102.             if (!FTPReply.isPositiveCompletion(reply)) {  
  103.                 ftp.disconnect();  
  104.                 return result;  
  105.             }  
  106.             ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录  
  107.             FTPFile[] fs = ftp.listFiles();  
  108.             for (FTPFile ff : fs) {  
  109.                 if (ff.getName().equals(fileName)) {  
  110.                     File localFile = new File(localPath + "/" + ff.getName());  
  111.   
  112.                     OutputStream is = new FileOutputStream(localFile);  
  113.                     ftp.retrieveFile(ff.getName(), is);  
  114.                     is.close();  
  115.                 }  
  116.             }  
  117.   
  118.             ftp.logout();  
  119.             result = true;  
  120.         } catch (IOException e) {  
  121.             e.printStackTrace();  
  122.         } finally {  
  123.             if (ftp.isConnected()) {  
  124.                 try {  
  125.                     ftp.disconnect();  
  126.                 } catch (IOException ioe) {  
  127.                 }  
  128.             }  
  129.         }  
  130.         return result;  
  131.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柠檬不萌c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值