下载Java中的整个FTP目录(Apache Net Commons) [英] Download entire FTP directory in Java (Apache Net Commons)

查看:84
本文介绍了下载Java中的整个FTP目录(Apache Net Commons)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归遍历登录FTP服务器后到达的整个根目录.

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

我能够连接,我真正想要做的就是遍历整个结构,然后下载每个文件和文件夹,并使其具有与FTP上相同的结构.到目前为止,我所能提供的是一种有效的下载方法,它将下载到服务器并获取我的整个文件结构,这很不错,只是它在第一次尝试中失败,然后在第二次尝试中失败了.我得到的错误如下:

I am able to connect, all I really want to do from there is recurse through the entire structure and and download each file and folder and have it in the same structure as it is on the FTP. What I have so far is a working download method, it goes to the server and gets my entire structure of files, which is brilliant, except it fails on the first attempt, then works the second time around. The error I get is as follows:

java.io.FileNotFoundException:输出目录\ test \ testFile.png (系统找不到指定的路径)

java.io.FileNotFoundException: output-directory\test\testFile.png (The system cannot find the path specified)

我设法完成了本地目录的上载功能,但是经过多次尝试后,我还是无法真正开始下载.

I managed to do upload functionality of a directory that I have locally, but can't quite get downloading to work, after numerous attempts I really need some help.

public static void download(String filename, String base)
{
    File basedir = new File(base);
    basedir.mkdirs();

    try
    {
        FTPFile[] ftpFiles = ftpClient.listFiles();
        for (FTPFile file : ftpFiles)
        {
            if (!file.getName().equals(".") && !file.getName().equals("..")) {
                // If Dealing with a directory, change to it and call the function again
                if (file.isDirectory())
                {
                    // Change working Directory to this directory.
                    ftpClient.changeWorkingDirectory(file.getName());
                    // Recursive call to this method.
                    download(ftpClient.printWorkingDirectory(), base);

                    // Create the directory locally - in the right place
                    File newDir = new File (base + "/" + ftpClient.printWorkingDirectory());
                    newDir.mkdirs();

                    // Come back out to the parent level.
                    ftpClient.changeToParentDirectory();
                }
                else
                {
                    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                    String remoteFile1 = ftpClient.printWorkingDirectory() + "/" + file.getName();
                    File downloadFile1 = new File(base + "/" + ftpClient.printWorkingDirectory() + "/" + file.getName());
                    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
                    boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
                    outputStream1.close();
                }
            }
        }
    }
    catch(IOException ex)
    {
        System.out.println(ex);
    }
}

推荐答案

您的问题(好吧,在我们摆脱了...并且克服了二进制问题之后,您当前遇到的问题)就是在调用newDir.mkdirs()之前执行递归步骤.

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().

所以假设您有一棵像

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

您要做的是跳过点文件,看到someDir是目录,然后立即进入其中,跳过其点文件,然后看到someFile.txt并进行处理.您尚未在本地创建someDir,因此会出现异常.

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.

因此,下次您运行程序时,本地someDir目录已经从上次运行中创建,您不会发现任何问题.

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();
            }

这篇关于下载Java中的整个FTP目录(Apache Net Commons)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆