2012-08-14 52 views
1

我正在做这个项目,我必须存储用户的信息,包括他的图像。 我将它作为blob图像存储在数据库中。 如果“照片”是我的图像列,比当我在网页上呈现其他列时,例如:“render.column_name”它工作正常。但是当我为图像做这件事时,像“render.photo”,DB在我的网页中返回了一些“jpa.blob .....”。有人可以帮助我如何显示存储在数据库中的这个图像?在Play 1.2.4框架中显示BLOB图像

回答

3

您需要一个单独的控制器方法来提供图像数据。控制器可能看起来像这样,假设您有一些关于该文件的元信息,即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)}" />