-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
  • 166,747
  • 40
  • 210
  • 420
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
  • 612
  • 10
  • 17
Sunil Gulabani
  • 838
  • 1
  • 7
  • 20