简体   繁体   English

如何确保通过Java中的InputStream将byte []保存为jpg图像?

[英]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 . 我正在尝试使用Apache Commons FTPClient.storeFile(...)方法将图像保存到FTPServer中,该方法需要InputStream The image comes as a Byte[] at the beginning. 图像开头是Byte[]

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 ! 因此,作为VGR accurately pointed out, the Apache Commons ftpClient has two working modes, ASCII and binary mode. 准确指出,Apache Commons ftpClient具有两种工作模式,ASCII和二进制模式。 By default the ASCII mode is used and one must enforce the treatment of data as binary data explicitly via 默认情况下,使用ASCII模式,并且必须通过以下方式显式强制将数据视为二进制数据

    try {            
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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