将图像保存为 BLOB 时如何在 JSP 中以文本形式显示它?
我已经做了很多谷歌搜索,但我无法找到具体的答案。
我正在使用 Spring MVC 3 将用户图像保存到数据库中。我现在能够成功地做到这一点。我将图像保存为 BLOB
。
我可以以 byte[]
形式检索图像。
无论文件类型如何 - jpg、png、gif 等(我的图像上传与图像文件类型无关)我希望允许 jpg、gif 和 png 进行渲染,即我的显示技术不应硬编码为仅显示一种类型对于图像,例如 jpg,它应该能够显示以各自类型上传的所有图像 - 这是要求。
现在,如果需要的话,我想做两件事来
- 调整图像的大小。 IE。 200 x 200
- 在带有文本的 JSP 中渲染图像。 这是一个用户个人资料页面,所以我 需要同时有文字和图像 显示。
spring mvc 如何渲染带有文本的图像?
我从我的研究中了解到,您可以为 jsp 使用 BufferedImage
类型?但我的问题是,似乎只能在内容类型严格为 image/jpeg
、image/gif
的情况下使用它。
我发现了一些调整大小的链接:
http://forum.springsource.org/archiv...p/t -46021.html
如果这些有效,欢迎任何建议,但最终我需要显示图像。
请传递您的想法。
谢谢。
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 respective 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.html
any suggestions welcome if these work BUT ultimately I need to display the image.
Please pass your thoughts along.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需创建一个 servlet,将图像从数据库流式传输到响应的输出流。然后,您可以按照通常的 HTML 方式调用它,如下所示:
如您所见,您只需在 HTML 中显示图像旁边的文本即可。无论如何,您不能将它们混合在单个 HTTP 响应中。图像算作单独的 HTTP 请求。有关此类 servlet 的更多详细信息和启动代码示例,请查看此答案。
至于调整大小,请查看 Java 2D API。
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:
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.