简体   繁体   English

Java Apache FTPClient 大多数下载的文件为空或丢失

[英]Java Apache FTPClient most downloaded files are empty or missing

This is my code that is supposed to download entire FTP directory to a local folder.这是我的代码,应该将整个 FTP 目录下载到本地文件夹。 It does it well, but most of the files are 0KB in size.它做得很好,但大多数文件的大小为 0KB。 Only JSON files seem to contain all their data.只有 JSON 文件似乎包含所有数据。

Things I tried:我尝试过的事情:

  1. Changing FTP file type with client.setFileType("FTP.BINARY_FILE_TYPE");使用client.setFileType("FTP.BINARY_FILE_TYPE");更改 FTP 文件类型client.setFileType("FTP.BINARY_FILE_TYPE");
  2. Using OutputStream instead of FileOutputStream使用OutputStream而不是FileOutputStream

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.由于某种原因,只有两个文件被复制 – https://pastebin.com/XNWqRMDj它们不为空。

The problem is your changeWorkingDirectory call.问题是您的changeWorkingDirectory调用。 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:有关在 Java 中下载 FTP 文件夹的完整工作代码,请参阅:
Download all folders recursively from FTP server in Java用Java从FTP服务器递归下载所有文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM