【问题标题】:Display BLOB images in Play 1.2.4 Framework在 Play 1.2.4 框架中显示 BLOB 图像
【发布时间】:2012-08-14 12:49:53
【问题描述】:
我正在做这个项目,我必须存储用户的信息,包括他的图像。
我将它作为 blob 图像存储在数据库中。
如果“照片”是我的图像列,而不是当我在网页中呈现其他列时,例如:“render.column_name”它工作正常。但是当我为图像执行此操作时,例如“render.photo”,数据库在我的网页中返回“jpa.blob .....”。有人可以帮我如何显示存储在数据库中的这张图片吗?
【问题讨论】:
标签:
postgresql
playframework
playframework-1.x
【解决方案1】:
您需要一个单独的控制器方法来提供图像数据。控制器可能看起来像这样,假设您有一些关于文件的元信息,即 mime 类型,这里是自定义实体 FileStore。
public class ShowFile extends Controller {
public static void render(String id) {
FileStore fs = // fetch the file out of the DB using the id
notFoundIfNull(fs);
response.setContentTypeIfNotSet(fs.mimeType);
response.setHeader("Cache-Control", "public, max-age=31536000");
renderBinary(new ByteArrayInputStream(fs.content), fs.content.length);
}
}
在页面中,您将使用img 标签链接到该方法:
<img src="@{ShowFile.render(image.id)}" />