Apache Commons Net FTP is uploading corrupted files
Solution
I had the same issue and solved it by calling
ftpClient.setFileType(FTP.BINARY_FILE_TYPE)
before each method retrieveFile
, retrieveFileStream
, storeFile
Explanation
File is corrupted, because default fileType is FTP.ASCII_FILE_TYPE
. This causes the issue. If you are on linux all bytes \n\r
(windows end of file) are changed into \n
byte. And this corrupt the file.
To avoid this behavior you have to call ftpClient.setFileType(FTP.BINARY_FILE_TYPE)
. Unfortunately, this setup is reset by each connect
method back to ASCII_FILE_TYPE
.
In my case this was reset even by method listFiles
. I guess, that this happened because I use passiveMode
on ftpClient.
So if you want to avoid troubles call setFileType(FTP.BINARY_FILE_TYPE)
right before every file transfer.
Commons FTP defaults to Ascii file types. You want to set it to Binary when dealing with binary data like a ZIP file.
From http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html
The default settings for FTPClient are for it to use FTP.ASCII_FILE_TYPE , FTP.NON_PRINT_TEXT_FORMAT , FTP.STREAM_TRANSFER_MODE , and FTP.FILE_STRUCTURE . The only file types directly supported are FTP.ASCII_FILE_TYPE and FTP.BINARY_FILE_TYPE .
You want to do setFileType(FTP.BINARY_FILE_TYPE)
before you send the file.