Java Reading/Loading 一张图片

Java Reading/Loading an Image

我有一个基于 Spring Web 模型-视图-控制器 (MVC) 框架的项目。 Spring Web 模型-视图-控制器 (MVC) 框架的版本是 3.2.8。 我正在阅读图片

public static void main(String[] args) {

    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("C:/tmp/device.jpg"));
    } catch (IOException e) {
    }       
    System.out.println  ("img -> " + img);
}

并且它运行良好,结果如下:

img -> BufferedImage@18e8541: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@1ce85c4 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 480 height = 640 #numDataElements 3 dataOff[0] = 2

但是当我在我的 Spring-MVC 应用程序中上传 相同 图像时:

 MultipartFile file = productForm.getAttachment();  

System.out.println ("productForm.getAttachment() ------------------> " + productForm.getAttachment());
        System.out.println ("productForm.getAttachment().getContentType() -> " + productForm.getAttachment().getContentType());
        System.out.println ("productForm.getAttachment().getSize() --------> " + productForm.getAttachment().getSize());

    byte[] result = new byte[(int) file.getSize()];
    Image img = new Image();
    img.setContent(result);
    ByteArrayInputStream in = new ByteArrayInputStream(img.getContent());
    BufferedImage img2 = null;
                try {
                    img2 = ImageIO.read(in);
                    System.out.println ("IMAGE CONTENT2 ------> " + img2);
                } catch (IOException e) {
                }

    productForm.getAttachment() ------------------> org.springframework.web.multipart.commons.CommonsMultipartFile@1f1bc67
    productForm.getAttachment().getContentType() -> image/jpeg
    productForm.getAttachment().getSize() --------> 28704

,我遇到过 img2 为空 !!!

这里是class图片

public class Image implements java.io.Serializable {

    private Long id;
    private byte[] content;

    public Image() {
    }

    public Image(Image image) {
        this.id = image.getId();
        this.content = image.getContent();
    }

    public Image(byte[] content) {
        this.content = content;
    }

    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 38, scale = 0)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "CONTENT", nullable = false)
    @Lob
    @Basic(fetch = FetchType.LAZY)
    public byte[] getContent() {
        return this.content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

}

问题很简单,result数组为空。如果你把代码改成下面这样,应该就可以了。

发件人:

MultipartFile file = productForm.getAttachment();  
...
byte[] result = new byte[(int) file.getSize()]; // Bug: Empty array
Image img = new Image();
img.setContent(result);
ByteArrayInputStream in = new ByteArrayInputStream(img.getContent());

收件人:

MultipartFile file = productForm.getAttachment();  
...
byte[] result = file.getBytes(); // Good: The content of the uploaded file
Image img = new Image();
img.setContent(result);
ByteArrayInputStream in = new ByteArrayInputStream(img.getContent());

PS:我仍然不确定 Image class 在这一切中做了什么,它应该与简单的工作一样好:

MultipartFile file = productForm.getAttachment();  
...
ByteArrayInputStream in = new ByteArrayInputStream(file.getBytes());

或者,更好的是,您可以将 file.getInputStream() 直接传递给 ImageIO.read(...),以避免在内存中缓存文件内容:

MultipartFile file = productForm.getAttachment();  
...
InputStream in = file.getInputStream();

除非您稍后使用 img,否则所有这一切,当然,为了简洁起见,将其排除在外。