Search code examples
javaftpapache-commonsftp-clientapache-commons-net

Apache Commons FTPClient Not Retrieving All Bytes From Source File


I have a fairly basic 3.8.0 FTPClient use case:

FTPClient ftpClient = new FTPClient()

ftpClient.connect(hostName, hostPort)

if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
  ftpClient.setFileType(FTP.BINARY_FILE_TYPE)
  ftpClient.login(userName, password)

  FileOutputStream fos = new FileOutputStream("localFile.txt")

  ftpClient.retrieve("remoteFile.zip", fos)
}

The problem I have is that the resulting local file is always ~1400 Bytes smaller than the remote source file:

source file size: 134,914,722
dest file size:   134,913,316

This doesn't seem to be a server problem since the standard linux ftp command retrieves the entire file.

It also doesn't seem to be OutputStream related because I have the same problem when I pull the file contents into memory:

//still too small
Byte[] fileContents = ftpClient.retrieveFileStream("remoteFile.zip").readAllBytes()

What would cause the apache commons FTPClient to allow connectivity and file download but not exhaustively retrieve all bytes from the source file?

Thank you in advance for your consideration and response.


Solution

  • The problem was the ordering of setFileType; it should have come after login instead of before.

    ftpClient.connect(...
    
    ftpClient.login(...
    
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE)
    

    The file coming in smaller was likely due to charset interpretations of the raw zip file.