2017-01-26 3 views
-2

Ich habe ein Projekt basierend auf Spring Web Model-View-Controller (MVC) Framework. Die Version des Model-View-Controller-Framework (MVC) von Spring Web ist 3.2.8. Ich lese ein BildJava Lesen/Laden eines Bildes

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

und es ist die Arbeit mit diesem Ergebnis gut:

img -> [email protected]: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = [email protected] transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 480 height = 640 #numDataElements 3 dataOff[0] = 2 

Aber wenn ich das gleiche Bild in meinem Frühlings-MVC-app laden:

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() ------------------> [email protected]f1bc67 
    productForm.getAttachment().getContentType() -> image/jpeg 
    productForm.getAttachment().getSize() --------> 28704 

, ich habe konfrontiert img2 ist NULL !!!

Hier ist die Klasse Bild

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

} 
+0

Haben Sie diese Datei überprüft, result und img ist nicht null, um sicherzustellen, dass das Problem nicht dort liegt. –

+0

hast du vielleicht eine ... 'IOException'? : D – fantaghirocco

+0

Nein, ich sehe IMAGE CONTENT2 ------> –

Antwort

0

Das Problem ist einfach, die result Array ist leer. Wenn Sie den Code wie folgt ändern, sollten Sie wahrscheinlich gut sein.

Von:

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

An:

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: Ich bin noch nicht sicher, was die Image Klasse in all dies zu tun, sollte es genauso gut funktionieren mit einfach:

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

Oder, noch besser, könnte man file.getInputStream() direkt an ImageIO.read(...) passieren, um zu verhindern den Inhalt der Datei im Speicher zwischenspeichern:

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

All dies ist, es sei denn Du img für etwas später zu verwenden, aber es einfach indiskutabel gelassen Kürze, natürlich.

Verwandte Themen