<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>
也可以看看: