Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload image in spring mvc

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.

like image 660
Shaik Mujahid Ali Avatar asked Oct 09 '14 10:10

Shaik Mujahid Ali


1 Answers

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}'/>" >
like image 157
krohit Avatar answered Oct 01 '22 04:10

krohit