java ftp commonsnet_用Java下载整个FTP目录(Apache Net Commons)
我試圖遞歸遍歷登錄到FTP服務器后到達的整個根目錄.
我能夠連接,所有我真正想要做的就是通過整個結構進行遞歸并下載每個文件和文件夾,并使其與FTP上的結構相同.到目前為止我所擁有的是一個有效的下載方法,它進入服務器并獲取我的整個文件結構,這很棒,除非它在第一次嘗試時失敗,然后第二次工作.我得到的錯誤如下:
java.io.FileNotFoundException: output-directoryestestFile.png
(The system cannot find the path specified)
我設法上傳了我在本地的目錄的上傳功能,但是經過多次嘗試我無法完全下載工作,我真的需要一些幫助.
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);
}
}
總結
以上是生活随笔為你收集整理的java ftp commonsnet_用Java下载整个FTP目录(Apache Net Commons)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WF4.0:NativeActivity
- 下一篇: Java类集框架 —— LinkedHa