Download entire FTP directory in Java (Apache Net Commons)

前端 未结 2 1956
名媛妹妹
名媛妹妹 2020-12-19 22:26

I am trying to recursively iterate through the entire root directory that I arrive at after login to the FTP server.

I am able to connect, all I really want to do fr

相关标签:
2条回答
  • 2020-12-19 22:53

    Your problem (well, your current problem after we got rid of the . and .. and you got past the binary issue) is that you are doing the recursion step before calling newDir.mkdirs().

    So suppose you have a tree like

    .
    ..
    someDir
       .
       ..
       someFile.txt
    someOtherDir
       .
       ..
    someOtherFile.png
    

    What you do is skip the dot files, see that someDir is a directory, then immediately go inside it, skip its dot files, and see someFile.txt, and process it. You have not created someDir locally as yet, so you get an exception.

    Your exception handler does not stop execution, so control goes back to the upper level of the recursion. At this point it creates the directory.

    So next time you run your program, the local someDir directory is already created from the previous run, and you see no problem.

    Basically, you should change your code to:

                if (file.isDirectory())
                {
                    // Change working Directory to this directory.
                    ftpClient.changeWorkingDirectory(file.getName());
    
                    // Create the directory locally - in the right place
                    File newDir = new File (base + "/" + ftpClient.printWorkingDirectory());
                    newDir.mkdirs();
    
                    // Recursive call to this method.
                    download(ftpClient.printWorkingDirectory(), base);
    
                    // Come back out to the parent level.
                    ftpClient.changeToParentDirectory();
                }
    
    0 讨论(0)
  • 2020-12-19 22:54

    A complete standalone code to download all files recursively from an FTP folder:

    private static void downloadFolder(
        FTPClient ftpClient, String remotePath, String localPath) throws IOException
    {
        System.out.println("Downloading folder " + remotePath + " to " + localPath);
    
        FTPFile[] remoteFiles = ftpClient.listFiles(remotePath);
    
        for (FTPFile remoteFile : remoteFiles)
        {
            if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals(".."))
            {
                String remoteFilePath = remotePath + "/" + remoteFile.getName();
                String localFilePath = localPath + "/" + remoteFile.getName();
    
                if (remoteFile.isDirectory())
                {
                    new File(localFilePath).mkdirs();
    
                    downloadFolder(ftpClient, remoteFilePath, localFilePath);
                }
                else
                {
                    System.out.println("Downloading file " + remoteFilePath + " to " +
                        localFilePath);
    
                    OutputStream outputStream =
                        new BufferedOutputStream(new FileOutputStream(localFilePath));
                    if (!ftpClient.retrieveFile(remoteFilePath, outputStream))
                    {
                        System.out.println("Failed to download file " + remoteFilePath);
                    }
                    outputStream.close();
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题