0 Replies Latest reply on Sep 24, 2002 4:30 AM by Dragomir Denev

    How to write an image to the response? The image is fetched

    Dragomir Denev Newbie

      Hi,

      Can somebody give me an example of how to write an image to the reponse?
      The image is fetched from a DB via a session bean. In the servlet doGet() I have:
      ...
      Object image = imageGetter.getImage(imageIdentifier); // imageGetter is a session bean
      response.setContentType("image/jpeg");
      ServletOutputStream out = response.getOutputStream();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(image);
      out.write(baos.toByteArray());
      oos.close();
      out.close();
      ...

      The image is a BLOB in the DB and is mapped to java.lang.Object, so i get it as Object.
      I'm not sure that this code is correct. Also I get an exception when I request the image:

      10:01:52,626 ERROR [Engine] StandardWrapperValve[ImageGetter]: Servlet.service() for servlet ImageGetter threw exception
      java.net.SocketException: Connection reset by peer: socket write error
      ...

      I request the image as 'http://localhost:8080/image?id=', 'image' is my servlet mapping.

      Can anyone give me a clue as to how to do the thing right.
      10x in advance.