Java Apache FTPClient 最常下载的文件为空或丢失。

huangapple go评论83阅读模式
英文:

Java Apache FTPClient most downloaded files are empty or missing

问题

这是我的代码,应该能够将整个FTP目录下载到本地文件夹。它确实可以正常运行,但大多数文件的大小为0KB。只有JSON文件似乎包含了所有数据。

我尝试过的方法:

  1. 使用 client.setFileType("FTP.BINARY_FILE_TYPE"); 更改FTP文件类型。
  2. 使用 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:

  1. Changing FTP file type with client.setFileType("FTP.BINARY_FILE_TYPE");
  2. Using OutputStream instead of 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.

答案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

huangapple
  • 本文由 发表于 2020年10月26日 04:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64528401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定