Reputation: 353
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.
Upvotes: 2
Views: 16550
Reputation: 1724
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"
Upvotes: 2
Reputation: 4666
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.
Upvotes: 3