問(wèn)題描述
我正在嘗試遞歸遍歷登錄 FTP 服務(wù)器后到達(dá)的整個(gè)根目錄.
I am trying to recursively iterate through the entire root directory that I arrive at after login to the FTP server.
我能夠連接,我真正想做的就是遞歸整個(gè)結(jié)構(gòu),下載每個(gè)文件和文件夾,并使其具有與 FTP 上相同的結(jié)構(gòu).到目前為止,我所擁有的是一種有效的下載方法,它進(jìn)入服務(wù)器并獲取我的整個(gè)文件結(jié)構(gòu),這非常棒,除了它在第一次嘗試時(shí)失敗,然后在第二次嘗試.我得到的錯(cuò)誤如下:
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(系統(tǒng)找不到指定的路徑)
java.io.FileNotFoundException: output-directory est estFile.png (The system cannot find the path specified)
我設(shè)法完成了本地目錄的上傳功能,但無(wú)法完全下載工作,經(jīng)過(guò)多次嘗試,我真的需要一些幫助.
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);
}
}
推薦答案
你的問(wèn)題(嗯,在我們?nèi)サ?.
和 ..
之后你現(xiàn)在的問(wèn)題和您解決了二進(jìn)制問(wèn)題)是您在調(diào)用 newDir.mkdirs()
之前正在執(zhí)行遞歸步驟.
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()
.
假設(shè)你有一棵樹(shù)
.
..
someDir
.
..
someFile.txt
someOtherDir
.
..
someOtherFile.png
你所做的是跳過(guò)點(diǎn)文件,看到 someDir
是一個(gè)目錄,然后立即進(jìn)入其中,跳過(guò)它的點(diǎn)文件,然后查看 someFile.txt
,并處理它.你還沒(méi)有在本地創(chuàng)建 someDir
,所以你得到了一個(gè)異常.
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.
您的異常處理程序不會(huì)停止執(zhí)行,因此控制會(huì)返回到遞歸的上層.此時(shí)它會(huì)創(chuàng)建目錄.
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.
所以下次你運(yùn)行你的程序時(shí),本地的someDir
目錄已經(jīng)從之前的運(yùn)行中創(chuàng)建好了,你看不出有什么問(wèn)題.
So next time you run your program, the local someDir
directory is already created from the previous run, and you see no problem.
基本上,您應(yīng)該將代碼更改為:
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();
}
這篇關(guān)于用 Java 下載整個(gè) FTP 目錄(Apache Net Commons)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!