如何使用 Struts 2 向浏览器提供图像? 休眠3?
我正在使用 Struts 2.1.2 和 Hibernate 3.2.6.GA 开发一个 Web 应用程序。 我有一个实体 User
,我已使用 Hibernate 将其映射到数据库中的表 USERS
。 我想要一个与该实体关联的图像,我计划将其作为 BLOB 存储在数据库中。 我还想在网页上显示图像以及 User
的其他属性。
我能想到的解决方案是有一个表IMAGES(ID, IMAGE)
,其中IMAGE
是一个BLOB
列。 USERS
将有一个名为 IMAGEID
的 FK
列,它指向 IMAGES
表。 然后,我将在 User
实体上映射一个名为 imageId
的属性,并以 Long 形式映射到此 IMAGEID
。 使用 JSP 渲染页面时,我会将图像添加为 等,并有一个读取图像并流式传输的操作内容发送到浏览器,并设置标头以长时间缓存图像。
这行得通吗? 是否有更好的方法来渲染存储在数据库中的图像? 首先将此类图像存储在数据库中是正确的方法吗?
I am developing a web application using Struts 2.1.2 and Hibernate 3.2.6.GA. I have an entity, User
, which I have mapped to a table USERS
in the DB using Hibernate. I want to have an image associated with this entity, which I plan to store as a BLOB
in the DB. I also want to display the image on a webpage along with other attributes of the User
.
The solution I could think of was to have a table IMAGES(ID, IMAGE)
where IMAGE
is a BLOB
column. USERS
will have an FK
column called IMAGEID
, which points to the IMAGES
table. I will then map a property on User
entity, called imageId
mapped to this IMAGEID
as a Long. When rendering the page with a JSP, I would add images as <img src="images.action?id=1"/>
etc, and have an Action which reads the image and streams the content to the browser, with the headers set to cache the image for a long time.
Will this work? Is there a better approach for rendering images stored in a DB? Is storing such images in the DB the right approach in the first place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您建议的解决方案会起作用。 鉴于您正在 Java 环境中工作,将图像存储在数据库中是最好的方法。 如果您在单个服务器环境中运行,并且应用程序服务器允许您在技术上以分解格式进行部署,那么您可以将映像存储在磁盘上,但这不是最佳实践。 一种建议是使用 servlet 而不是 JSP。 为了获得良好的浏览器行为,您希望浏览器认为它正在显示的文件类型与其期望的文件类型匹配。 尽管存在 mime 类型标头,但文件扩展名仍然非常重要。 因此,您需要一个如下所示的链接:
其中 123456789 是数据库中图像的主键。 您的 servlet 映射将如下所示:
然后,在您的 servlet 中,只需解析图像 ID 的请求 URL,而不是使用查询字符串,因为查询字符串会使某些浏览器感到困惑。 使用查询字符串不会彻底破坏浏览器,但您会在缓存方面遇到奇怪的行为,并且某些浏览器可能会将内容报告为不安全。
Yes your suggested solution will work. Given that you are working in a Java environment storing the images in the database is the best way to go. If you are running in a single server environment with an application server that will let you deploy in an exploded format technically you could store the images on disk but that wouldn't be the best practice. One suggestion would be to use a servlet instead of a JSP. To get good browser behavior you want the browser to think that the file type that it is displaying matches the file type that it is expecting. Despite the existence of mime type headers the file extension is still really important. So you want a link that looks like this:
Where 123456789 is the primary key of your image in the database. Your servlet mapping would look like this:
Then in your servlet simply parse the request URL for the image ID rather than using the query string as the query string will confuse some browsers. Using the query string won't break browsers outright but you'll get odd behavior with regards to caching and some browsers may report the content as unsafe.
Internet Explorer 不支持这种图像嵌入方式。
Internet Explorer does not support that style of image embedding.
如果您想直接显示用户图像及其属性,也许您可以考虑将图像数据直接嵌入 HTML 中。
使用特殊的 data: URL 方案,您可以在 HTML 页面中嵌入任何 mime 数据,格式如下:
需要替换为数据的 mime 类型(例如 image/png)并且
是文件实际字节的 Base64 编码字符串。
请参阅 RFC 2557。
示例:
If you want to display the user image directly with their properties perhaps you can consider embedding the image data directly in the HTML.
Using a special data: URL scheme you are able to embed any mime data inside a HTML page, the format is as follows:
needs to be replaced by the mime-type of your data (image/png for instance) and
is the base64 encoded string of the actual bytes of the file.
See RFC 2557.
Example:
您建议的解决方案将完美运行。 我也做过同样的事情。
但您不需要为此使用 servlet。
Struts2已经有一个流结果。
请参阅这个 Struts 2 示例,它准确地描述了您想要的内容。
Your suggested solution would work perfectly. I have done the same thing.
But you don't need a servlet for this.
Struts2 has already a stream result.
See this Struts 2 Example which describes exactly what you want.