Use ftpClient to download all files on ftp for parsing

  • 2020-07-21 08:13:24
  • OfStack

Requirement: The latest project needs, write a small function, the requirement is to download all files (including subdirectories) under the specified folder of ftp to the local folder in real time, keep the files to the directory path unchanged.

Analysis: The key is to live and download and keep the original directory. This is done in real time using timed scheduling of threads, mostly doing the latter, which obviously requires recursion, but files on ftp do not directly get relative paths (in the case of FTPClient, I don't have a method like getPath() in the FTPClient class), so paths are recorded by themselves. The general idea is as follows:

1. Get all paths and subpaths: recursively traverse all files to paths. Parameters: ftp is the FTPClient object, path is the current path, pathArray saves the current path and takes the collection of paths to the main function


getPath(ftp,path,pathArray);

 public static void getPath(FTPClient ftp,String path,ArrayList<String> pathArray) throws IOException{
    FTPFile[] files = ftp.listFiles();
    for (FTPFile ftpFile : files) {
      if(ftpFile.getName().equals(".")||ftpFile.getName().equals(".."))continue;
      if(ftpFile.isDirectory()){// If it's a directory, it recursively finds all the files in it 
        path+="/"+ftpFile.getName();
        pathArray.add(path);
        ftp.changeWorkingDirectory(path);// Change current path 
        getPath(ftp,path,pathArray);// Recursive calls 
        path=path.substring(0, path.lastIndexOf("/"));// To avoid interference with the subsequent path construction in the same directory, 
      }
    }
  }

2. Download to the specified local folder,

download (ftp pathArray, "c: \ \ download"); To check for errors, I divided the download into two parts. The first part created all the directories and downloaded the files in the second for loop. Parameters: ftp is FTPClient, pathArray is the set of paths taken out in 1, and the next String is the local path


 public static void download(FTPClient ftp,ArrayList<String> 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;// Construct local path 
      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();
        }
      }
    }
  }

Test the main function, use ftpClient for 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<String> pathArray=new ArrayList<String>();
    getPath(ftp,path,pathArray);
    System.out.println(pathArray);
    download(ftp, pathArray, "c:\\download");
    ftp.logout();
    ftp.disconnect();
  }

Related articles: