资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用ftpClient如何实现下载ftp文件解析

使用ftpClient如何实现下载ftp文件解析?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了明山免费建站欢迎大家使用!

总体思路有以下:

1、得到所有路径以及子路径:递归遍历所有文件到路径。参数:ftp为FTPClient对象,path为当前的路径,pathArray保存当前的路径,并将此路径集合带到主函数中去 

getPath(ftp,path,pathArray);
 public static void getPath(FTPClient ftp,String path,ArrayList pathArray) throws IOException{
    FTPFile[] files = ftp.listFiles();
    for (FTPFile ftpFile : files) {
      if(ftpFile.getName().equals(".")||ftpFile.getName().equals(".."))continue;
      if(ftpFile.isDirectory()){//如果是目录,则递归调用,查找里面所有文件
        path+="/"+ftpFile.getName();
        pathArray.add(path);
        ftp.changeWorkingDirectory(path);//改变当前路径
        getPath(ftp,path,pathArray);//递归调用
        path=path.substring(0, path.lastIndexOf("/"));//避免对之后的同目录下的路径构造作出干扰,
      }
    }
  }

2、下载到指定的本地文件夹中,

download(ftp, pathArray, "c:\\download");程序之前出了写错误,为了排查,我把下载分成两部分,第一部分先将所有目录创建完成,在第二个for循环中进行文件的下载。参数:ftp为FTPClient,pathArray为1中带出的路径集合,后面一个String为本地路径  

 public static void download(FTPClient ftp,ArrayList pathArray,String localRootPath) throws IOException{
    for (String string : pathArray) {
      String localPath=localRootPath+string;
      File localFile = new File(localPath);
      if (!localFile.exists()) { 
        localFile.mkdirs(); 
      }
    }
    for (String string : pathArray) {
      String localPath=localRootPath+string;//构造本地路径
      ftp.changeWorkingDirectory(string);
      FTPFile[] file=ftp.listFiles();
      for (FTPFile ftpFile : file) {
        if(ftpFile.getName().equals(".")||ftpFile.getName().equals(".."))continue;
        File localFile = new File(localPath);
        if(!ftpFile.isDirectory()){
          OutputStream is = new FileOutputStream(localFile+"/"+ftpFile.getName());
          ftp.retrieveFile(ftpFile.getName(), is);
          is.close();
        }
      }
    }
  }

测试的主函数,使用的ftpClient为org.apache.commons.net.ftp.FTPClient:

 public static void main(String[] args) throws SocketException, IOException {
    FTPClient ftp = new FTPClient();
    ftp.connect("127.0.0.1");
    ftp.login("test","test");
    int reply;
    reply = ftp.getReplyCode();
    if(!FTPReply.isPositiveCompletion(reply)) {
      ftp.disconnect();
      System.err.println("FTP server refused connection.");
      System.exit(1);
    }
    ftp.setBufferSize(1024);
    ftp.setFileType(FTP.BINARY_FILE_TYPE); 
    ftp.enterLocalPassiveMode();
    String path="";
    ArrayList pathArray=new ArrayList();
    getPath(ftp,path,pathArray);
    System.out.println(pathArray);
    download(ftp, pathArray, "c:\\download");
    ftp.logout();
    ftp.disconnect();
  }

关于使用ftpClient如何实现下载ftp文件解析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


文章名称:使用ftpClient如何实现下载ftp文件解析
路径分享:http://cdkjz.cn/article/jseshc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220