简体   繁体   English

从字节列DB获取p:graphicImage

[英]Getting p:graphicImage from byte column DB

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? 下面是从物理路径获取图形imagem的代码。如何从字节列中获取代码?

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

The <p:graphicImage> supports streaming content. <p:graphicImage>支持流内容。 All you need is the following application scoped bean which returns the desired image bytes from the DB based on an unique image identifier as request parameter: 您需要做的是以下应用程序范围的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()));
        }
    }

}

Here's how you can use it: 使用方法如下:

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

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM