I am using spring 4 and hibernate 4 to upload and retrieve image to and from database. I have converted multipart image into byte array and stored in database. My query is how to retrieve that image from database and display the byte array in jsp without storing it in local system.
As you haven't mentioned your DB structure for storing the images, I assume that you are storing it in blob
datatype.
Part 1: ControllerClass
After retrieving the image from the DB, you have to encode that image using Base64.encode
and map that image to your jsp (using java.util.map
).
Map<String, Object> model = new HashMap<String, Object>();
model.put("myImage", Base64.encode(MyImage)); //MyImage (datatype 'byte[]') is the image retrieved from DB
return new ModelAndView("display", model); //display is the name of jsp on which you want to display image
Part 2: JSP
Then display it on the JSP
by decoding the byte array,
<img id="myImg" name="myImg" src="data:image/jpg;base64,<c:out value='${myImage}'/>" >
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With