英文:
Java Apache FTPClient most downloaded files are empty or missing
问题
这是我的代码,应该能够将整个FTP目录下载到本地文件夹。它确实可以正常运行,但大多数文件的大小为0KB。只有JSON文件似乎包含了所有数据。
我尝试过的方法:
- 使用 
client.setFileType("FTP.BINARY_FILE_TYPE");更改FTP文件类型。 - 使用 
OutputStream替代FileOutputStream。 
代码部分:
public static void copyFolder(File destination, FTPFile sourceFile, FTPClient ftpClient) throws IOException{
    if (!sourceFile.isDirectory()) {
        // 复制文件
        File downloadFile = new File(destination + "/" + sourceFile.getName());
        String remoteFile =  sourceFile.getName();
        FileOutputStream outputStream = new FileOutputStream(downloadFile);
        System.out.println(remoteFile);
        System.out.println(downloadFile.getPath());
        boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
        if(success) {
            System.out.println("已检索 " + remoteFile);
        }
        outputStream.close();
    }else{
        // 遍历子目录
        ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory() + "/" + sourceFile.getName());
        System.out.println(ftpClient.printWorkingDirectory());
        FTPFile[] contents = ftpClient.listFiles(ftpClient.printWorkingDirectory());
        File newDest = new File(destination + "/" + sourceFile.getName());
        if(!newDest.exists()){
            newDest.mkdir();
        }
        for(FTPFile file : contents){
            copyFolder(newDest, file, ftpClient);
        }
        return;
    }
}
如何正确进行传输?
在同一台计算机上尝试下载时,会在文件下载期间和之间多次失去连接。而且似乎只下载了少数文件。我将更改问题的标题,使其更具体。
由于某种原因,只有两个文件被复制 - https://pastebin.com/XNWqRMDj 它们并不为空。
英文:
This is my code that is supposed to download entire FTP directory to a local folder. It does it well, but most of the files are 0KB in size. Only JSON files seem to contain all their data.
Things I tried:
- Changing FTP file type with 
client.setFileType("FTP.BINARY_FILE_TYPE"); - Using 
OutputStreaminstead ofFileOutputStream 
Code:
public static void copyFolder(File destination, FTPFile sourceFile, FTPClient ftpClient) throws IOException{
    if (!sourceFile.isDirectory()) {
        //copy file
        File downloadFile = new File(destination + "/"+ sourceFile.getName());
        String remoteFile =  sourceFile.getName();
        FileOutputStream outputStream = new FileOutputStream(downloadFile);
        System.out.println(remoteFile);
        System.out.println(downloadFile.getPath());
        boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
        if(success) {
            System.out.println("Retrieved " + remoteFile);
        }
        outputStream.close();
    }else{
        //loop through a subdirectory
        ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory() + "/" + sourceFile.getName());
        System.out.println(ftpClient.printWorkingDirectory());
        FTPFile[] contents = ftpClient.listFiles(ftpClient.printWorkingDirectory());
        File newDest = new File(destination + "/" + sourceFile.getName());
        if(!newDest.exists()){
            newDest.mkdir();
        }
        for(FTPFile file : contents){
            copyFolder(newDest, file, ftpClient);
        }
        return;
    }
}
How to get the transfer correctly?
Trying to download it on the same computer ended with losing connection a few times - between and during file downloads. Also it seems that few files are downloaded. I will change the title of question to be more specific.
Only two files are being copied for some reason – https://pastebin.com/XNWqRMDj They are not empty.
答案1
得分: 0
问题在于你的changeWorkingDirectory调用。它大多数情况下都失败。
ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory() + "/" + sourceFile.getName());
应改为:
ftpClient.changeWorkingDirectory(destination + "/" + sourceFile.getName());
要获取Java中下载FTP文件夹的完整可工作代码,请参阅:
https://stackoverflow.com/q/50398695/850848
英文:
The problem is your changeWorkingDirectory call. It's failing most of the time.
ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory() + "/" + sourceFile.getName());
It should be:
ftpClient.changeWorkingDirectory(destination + "/" + sourceFile.getName());
For a complete working code for downloading FTP folders in Java, see:
https://stackoverflow.com/q/50398695/850848
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论