Spring MVC 图像控制器,用于在 JSP 中显示图像字节
我有一个 Spring MVC 应用程序,其中一个 JSP 必须显示来自数据库的图像。
图像作为 Blob 存储在数据库中。
显示它们的最简单方法是什么?我需要什么样的 servlet/控制器来在 JSP 上显示图像字节?应该是一个简单的问题,但我无法在任何地方找到完整的解决方案。
我的理解是,我需要一个单独的控制器,比如 ImgController/id=,它将根据请求参数显示图像字节,然后在我的 JSP 中我可以有 img src="ImgController/id..."。但是我如何实现和连接这个控制器呢?
任何帮助或例子将非常感激。多谢。
I have a Spring MVC application, and one of the JSPs must show images coming from a database.
The images are stored in the DB as Blobs.
What is the easiest way to display them? What kind of servlet/controller do I need to show on the JSP the image bytes? Should be an easy question, but I haven't been able to find a full solution anywhere.
My understanding is that I need a separate controller, say ImgController/id=, which will display the image bytes based on a request parameter, and then in my JSP I can have img src="ImgController/id...". But how do I implement and wire this controller?
Any help or example would be really appreciated. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的解决方案(尽可能少写)是带有返回类型 OutputStream 或 ResponseEntity 的 Spring MVC 控制器方法。
我更喜欢:返回一个 ResponseEntity:
但还有更多:
直接使用 HttpServletResponse
The simplest solution (in terms so writing as less as possible) is a Spring MVC Controller Method with return Type OutputStream or ResponseEntity.
I prefer: return a ResponseEntity:
But there are many more:
Use HttpServletResponse directly
我在这里编译了一些代码!
请查看并告诉我是否适合您的情况。我认为这应该有效。
I have compiled some code here!
Please have a look and let me know if that fits your case. I think this should work.