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