問題描述
我正在嘗試遞歸遍歷登錄 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();
}
這篇關于用 Java 下載整個 FTP 目錄(Apache Net Commons)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!