1

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

4

1 回答 1

3

You need a separate controller method to supply the image data. The controller could look like this, assuming you have some meta-information about the file, namely, the mime-type, here in a custom entity 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);
    }
}

In the page you'll link to that method using the img tag:

<img src="@{ShowFile.render(image.id)}" /> 
于 2012-08-14T13:20:17.840 回答