When saving an image as BLOB how to display it in JSP with text?
I have done a lot of googling but I have not been able to find a concrete answer.
I am using Spring MVC 3 to save a user image to the database. I am now successfully able to do that. I am saving the image as a BLOB
.
I can retrieve the image as a byte[]
.
Irregardless of file type - jpg, png, gif etc (My image upload is image file type agnostic) I would like to allow jpg and gif and png lets say to render i.e. my display technology should not be hard-coded to display just one type of image, say jpg, it should be able to display all images as they were uploaded in their respectiv开发者_如何学Ce types - that is the requirement.
NOW, I would like to do 2 things
- re-size the image if I have to. ie. 200 by 200
- render the image in a JSP WITH text. This is a user profile page so I need to have both text and image shown.
How can spring mvc render the image WITH text?
I understand from my research that you can use a BufferedImage
type for the jsp? but my problem is that it seems that you can only use that if the content type is strictly image/jpeg
, image/gif
.
I have come across some links for resizing:
http://forum.springsource.org/archiv...p/t-46021.htmlany suggestions welcome if these work BUT ultimately I need to display the image.
Please pass your thoughts along.
Thank you.
Just create a servlet which streams the image from the DB to the outputstream of the response. Then you can just call it the usual HTML way as follows:
<p>
<img src="imageservlet/${bean.imageId}" />
${bean.text}
</p>
As you see, you just display the text next to the image in HTML. You cannot mix them in a single HTTP response anyway. Images counts as separate HTTP requests. For more detail and a kickoff code example of such a servlet, check this answer.
As to resizing, checkout the Java 2D API.
精彩评论