简体   繁体   中英

How to ensure that byte[ ] is saved as a jpg image through an InputStream in java?

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM