-3

I have a InputStream of an image in string format. How to display that image in the browser using servlets?

This is the (start of the) string.

/9j/4AAQSkZJRgABAgAAAQABAAD/4QDVRXhpZgAASUkqAAgAAAAIABIBAwABAAAAAQAAABoBBQABAAAAbgAAABsBBQABAAAAdgAAACgBAwABAAAAAgAAADEBAgANAAAAfgAAADIBAgAUAAAAiwAAABMCAwABAAAAAQAAAGmHBAABAAAAnwAAAAAAAABkAAAAAQAAAGQAAAABAAAAQ...
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Narasimha
  • 39
  • 2
  • 11

1 Answers1

3

You need write the image as a byte array to the response's output stream. Something like this:

byte[] imageBytes = getImageAsBytes();
response.setContentType("image/jpeg");
response.setContentLength(imageBytes.length);
response.getOutputStream().write(imageBytes);

Then in you JSP you just use a standard img element:

<img src="url to your servlet">

Source: https://stackoverflow.com/a/1154279/1567585

hsusanoo
  • 774
  • 7
  • 18
Sunil Gulabani
  • 878
  • 1
  • 8
  • 21