![](/img/trans.png)
[英]Display dynamic image from database or remote source with p:graphicImage and StreamedContent
[英]Getting p:graphicImage from byte column DB
我需要顯示數據庫中字節列的圖形圖像。 下面是從物理路徑獲取圖形imagem的代碼。如何從字節列中獲取代碼?
<h:panelGrid columns="1" style="width:100%" cellpadding="5">
<p:graphicImage value="/images/cars/test.jpg"/>
</h:panelGrid>
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.