I am trying to save an image into an FTPServer using Apache Commons FTPClient.storeFile(...)
method, which requires an InputStream
. The image comes as a Byte[]
at the beginning.
Here some code:
InputStream is = new ByteArrayInputStream(byteArray);
boolean done = ftpClient.storeFile(remotePath, is);
However, when I download the uploaded image, it looks very strange and even though the dimensions are respected, the image does not look like it should. The Image after uploading Looks like this: Image after uploading In reality I do not have access to the original Image but I know it is an Image of the open sea with blue water on it. Thank you!
So, as VGR! accurately pointed out, the Apache Commons ftpClient has two working modes, ASCII and binary mode. By default the ASCII mode is used and one must enforce the treatment of data as binary data explicitly via
try {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
}