我正在尝试递归遍历登录FTP服务器后到达的整个根目录。我已经成功连接,但我真正想做的是递归遍历整个结构并下载每个文件和文件夹,并使其与FTP上的结构相同。我已经编写了一个可行的下载方法,它可以访问服务器并获取我的所有文件结构,这很棒,但第一次尝试失败,第二次尝试才能成功。我得到的错误如下:
java.io.FileNotFoundException: output-directory\test\testFile.png
(系统找不到指定的路径)
我已经成功上传了本地目录的功能,但无法使下载工作,在多次尝试后我真的需要一些帮助。
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);
}
}
!=
比较字符串? - RealSkeptic