首页 > 其他 > 详细

从ftp拷贝文件

时间:2015-10-09 18:33:24      阅读:400      评论:0      收藏:0      [点我收藏+]
public static File[] copyDir(String ftpIp, int ftpPort, String ftpUser, String ftpPass,
    String ftpPath, String tmpPath) throws Exception {
  
  FTPClient ftp = new FTPClient();
  try {
    ftp.connect(ftpIp, ftpPort);

    ftp.login(ftpUser, ftpPass);
    if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
      throw new Exception("用户名或密码不正确。");
    }

    FTPFile[] files = ftp.listFiles(ftpPath);
    if (files.length == 0 && ftp.getReplyString().indexOf("fail") > 0) {
      throw new Exception("目录不存在。");
    }

    File tmpDir = new File(tmpPath);
    if (!tmpDir.exists()){
      tmpDir.mkdirs();
    }
    for (FTPFile f : files) {
      System.out.println(f.getName());
      ftp.retrieveFile(ftpPath + "/" + f.getName(),
          new FileOutputStream(tmpDir.getAbsolutePath() + File.separator + f.getName()));
    }

    ftp.logout();

    return tmpDir.listFiles();
    
  } finally {
    if (ftp.isConnected()) {
      try {
        ftp.disconnect();
      } catch (Exception e) {
      }
    }
  }

}


从ftp拷贝文件

原文:http://my.oschina.net/h2do/blog/514938

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!