简体   繁体   中英

Java Reading/Loading an Image

I have a project based on Spring Web model-view-controller (MVC) framework. The version of the Spring Web model-view-controller (MVC) framework is 3.2.8. I am reading an image

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

and It is working fine with this result:

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

But when I upload the same image in my Spring-MVC app:

 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

, I have faced img2 is NULL !!!

Here the class Image

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

}

The problem is simple, the result array is empty. If you change the code as follows, you should probably be good.

From:

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

To:

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: I'm still not sure what the Image class is doing in all this, it should work just as fine with simply:

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

Or, even better, you could pass file.getInputStream() directly to ImageIO.read(...) , to avoid caching the file contents in memory:

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

All this is unless you use img for something later, but just left it out of the question for brevity, of course.

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