Issue
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...
Solution
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
Answered By - Sunil Gulabani
Answer Checked By - Senaida (JavaFixing Volunteer)