用 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: 输出目录 est estFile.png(系统找不到指定的路径)
java.io.FileNotFoundException: output-directory est estFile.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();
}
相关文章