0

Hi I'm coding servlet to upload image as inputstream under an object with static inputream.

After images uploaded, I am using some method to resize the image and return the resized image to the client.

My code work for the first time and after that the inputstrem is closed(i don't know exactly what this means and I can't read from it.

I have used several methods but still having the same problem

How to resolve this?

    public String getImageArtRes() throws IOException {
    ImageInputStream origimg = 
    ImageIO.createImageInputStream(this.get_Image());

    BufferedImage img = ImageIO.read(origimg);
    img.flush();

    BufferedImage resized = resize(img, 80, 120);
    String b64 = null;
    resized.flush();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(resized, this.getImgType().replace("image/", ""), os);
    os.flush();

    byte[] imageInByteArray = os.toByteArray();
    b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);
    os.close();

    return b64;
}

I am getting image with this code:

    private static List<ArticleIMG> getImages(HttpServletRequest request) throws IOException, ServletException {
    Collection<Part> parts = request.getParts();
    InputStream img = null;
    ...
    for (Part part : parts) {
    ...
                img=part.getInputStream();
                imgTemp.setImage(img);                    
                getImagesArt().add(imgTemp);
    ...

My java version is 1.8

Tomasz Bawor
  • 1,447
  • 15
  • 40

1 Answers1

0

because img in my code ,that i used to read from HTTP reqeut,is temp,,then the solution that i found is to convert InputStream to buffueredimage :

...
    InputStream img = null;
    BufferedImage image = null;
...
    img=part.getInputStream();

    image = ImageIO.read(img);
    imgTemp.setImage(image);                    
    getImagesArt().add(imgTemp);
...

other thing ,IOImage has problem with jpg and the solution for me is to use this method https://stackoverflow.com/a/23011325/1435391