0

I need to show a graphic image from a byte column in database. Below I have the code for a graphic imagem from a physical path.. how can I do it from a byte column?

<h:panelGrid columns="1" style="width:100%" cellpadding="5">  
        <p:graphicImage value="/images/cars/test.jpg"/>                 
</h:panelGrid> 
4

1 回答 1

1

<p:graphicImage>支持流媒体内容。您所需要的只是以下应用程序范围的 bean,它基于唯一的图像标识符作为请求参数从数据库返回所需的图像字节:

@Named
@ApplicationScoped
public class ImageStreamer {

    @EJB
    private ImageService service;

    public StreamedContent getImage() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        } else {
            // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
            String imageId = context.getExternalContext().getRequestParameterMap().get("imageId");
            Image image = imageService.find(Long.valueOf(imageId));
            return new DefaultStreamedContent(new ByteArrayInputStream(image.getBytes()));
        }
    }

}

以下是您可以使用它的方法:

<p:graphicImage value="#{imageStreamer.image}">
    <f:param name="id" value="#{someBean.imageId}" />
</p:graphicImage>

也可以看看:

于 2013-11-01T18:58:26.067 回答