Search code examples
javadownloadftpapache-commons-net

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. 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.


Solution

  • 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:
    Download all folders recursively from FTP server in Java