Abdus Samad
Abdus Samad

Reputation: 353

show image from DB using spring MVC

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

Answers (2)

Raj
Raj

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

Related Questions