2

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 2

3

You have some infos based on the sample spring mvc application Image Db:

Hope it helps.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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?
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/…
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
2

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

Can you please post your complete code of JSP file , Control File and tweet Entity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.