FtpClient和FTPClient下载的使用

最近接收一个遗留项目,用的是flex和java,后台用的是mybatis和spring。在测试时发现下载有问题,结果花了一两天时间才将问题解决,下面将解决过程中碰到的问题和解决的思路贴出来:
因为项目做出来有段时间了,当初用的是sun.net.ftp.FtpClient这个类,这个类本身就存在问题,而且java api文档中无法查询。并且是jdk1.6的版本,导致放在jre1.7上时保存,后来改成jre1.6问题才解决,但是仍然无法下载,一直报异常“Source not found for $$FastClassByCGLIB$$7782d62a.invoke(int, Object, Object[]) line: not available”。让人很纠结,硬是没有找到原因所在,网上有的说是sql语句错了,有的说是jar包的问题,有的说是反射或cglib的问题等等,但是一一测试仍然没有解决,因为之前有人说这个功能是好的,所以就没有打算换方法,后来实在不行,网上也有人建议用apache的org.apache.commons.net.ftp.FTPClient类,就换成了FTPClient。结果居然好用了:
代码如下:
Java代码 复制代码  收藏代码
  1. public static boolean downFile(List<String> directoryList,   
  2.             String targetPath) {     
  3.                
  4.             boolean success = false;     
  5.             for (String directoryString : directoryList) {   
  6.                 String fileName = directoryString.substring(directoryString   
  7.                         .lastIndexOf("/") + 1);   
  8.                 String remotePath = directoryString.substring(1, directoryString   
  9.                         .lastIndexOf("/"));   
  10.                  FTPClient ftp = new FTPClient();     
  11.                     try {     
  12.                         int reply;     
  13.                         ftp.connect(ftpIP);     
  14.                         ftp.login(username, password);//登录     
  15.                         reply = ftp.getReplyCode();     
  16.                         if (!FTPReply.isPositiveCompletion(reply)) {     
  17.                             ftp.disconnect();     
  18.                             return success;     
  19.                         }     
  20.                         ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录     
  21.                         FTPFile[] fs = ftp.listFiles();     
  22.                         for(FTPFile ff:fs){     
  23.                             if(ff.getName().equals(fileName)){     
  24.                                      
  25.                                 File localFile = new File(targetPath+"/");   
  26.                                 if (!localFile.exists()) {   
  27.                                     localFile.mkdirs();   
  28.                                 }   
  29.                                 OutputStream is = new FileOutputStream(localFile+"/"+ff.getName());     
  30.                                 ftp.retrieveFile(ff.getName(), is);     
  31.                                 is.close();     
  32.                             }     
  33.                         }     
  34.                              
  35.                         ftp.logout();     
  36.                         success = true;     
  37.                     } catch (IOException e) {     
  38.                         e.printStackTrace();     
  39.                     } finally {     
  40.                         if (ftp.isConnected()) {     
  41.                             try {     
  42.                                 ftp.disconnect();     
  43.                             } catch (IOException ioe) {     
  44.                             }     
  45.                         }     
  46.                     }     
  47.             }   
  48.             return success;     
  49.         }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值