Is there an example anywhere on how to display an image stored in a DB using spring MVC? I want to look at how controller is written and also how a JSP is written to show the image. This is to show the image, not just download it.
2 Answers
You have some infos based on the sample spring mvc application Image Db:
http://blog.springsource.com/2007/11/14/annotated-web-mvc-controllers-in-spring-25/
http://www.roseindia.net/tutorial/spring/spring3/web/spring-3-mvc-fileupload-example.html
Hope it helps.
4 Comments
Abdus Samad
I have seen these samples, but i would like to confirm few important steps to make sure that is the ONLY way to do it, 1.upload a file using multipart and store in DB as blob 2.When you want to show it in a page, goto the controller 3.controller will read the blob from DB 4.store the blob as image file in webapps/images folder 5.then send a model parameter to jsp giving the path to the image i want to confirm point number 4 in particular. Is storing images into a temp folder the only way to show an image in jsp? Or is there a way to skip that step and show directly?
Abdus Samad
I have seen these samples, but i would like to confirm few important steps to make sure that is the ONLY way to do it, 1.upload a file using multipart and store in DB as blob 2.When you want to show it in a page, goto the controller 3.controller will read the blob from DB 4.store the blob as image file in webapps/images folder 5.then send a model parameter to jsp giving the path to the image. i want to confirm point number 4 in particular. Is storing images into a temp folder the only way to show an image in jsp? Or is there a way to skip that step and show directly?
Arnaud Gourlay
That's not the only solution. You can create a controller that will stream your picture in the jsp img tag. It's explained there stackoverflow.com/questions/6604509/…
Abdus Samad
Is there a more complete example somewhere? Looking at the code snippet in the link you provided i am really not able to get the complete picture on how it is working. But yes, i think the ResponseEntity is what i am looking for. But i am looking for complete JSP and controller code
here is another example . this way we can show images directly from db , no need to create temp images .
In Controller ==>
@RequestMapping(value="/getUserImage/{id}")
public void getUserImage(HttpServletResponse response , @PathVariable("id") int tweetID) throws IOException{
response.setContentType("image/jpeg");
byte[] buffer = tweetService.getTweetByID(tweetID).getUserImage();
InputStream in1 = new ByteArrayInputStream(buffer);
IOUtils.copy(in1, response.getOutputStream());
}
In JSP ==> we can use like this , In my cases I'm getting tweet list so inside foreach loop , it will display image for all the tweets .
<img src="getUserImage/<c:out value="${tweet.id}"/>.do"
1 Comment
Ghost Rider
Can you please post your complete code of JSP file , Control File and tweet Entity