在Play 1.2.4 Framework中显示BLOB图像

时间:2012-08-14 12:49:53

标签: postgresql playframework playframework-1.x

我正在做这个项目,我必须存储用户的信息,包括他的图像。 我将它作为blob图像存储在数据库中。 如果“照片”是我的图像列,那么当我在网页中渲染其他列时,例如:“render.column_name”它可以正常工作。但是,当我为图像执行此操作时,例如“render.photo”,数据库会在我的网页中返回“jpa.blob .....”。有人可以帮我如何显示存储在数据库中的这个图像吗?

1 个答案:

答案 0 :(得分:3)

您需要一个单独的控制器方法来提供图像数据。控制器看起来像这样,假设你有一些关于文件的元信息,即mime-type,这里是自定义实体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)}" />