@ManagedBean
@ApplicationScoped
public class MediaManager {
@EJB
private MediaService service;
public StreamedContent getStream() 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 media. Return a real StreamedContent with the media bytes.
String id = context.getExternalContext().getRequestParameterMap().get("id");
Media media = service.find(Long.valueOf(id));
return new DefaultStreamedContent(new ByteArrayInputStream(media.getBytes()));
}
}
}
1 回答
与
<p:graphicImage>
中的一样,value
属性可以指向返回StreamedContent
的bean属性 . 这只需要一个特殊的getter方法,原因在下面的答案中详细解释了使用<p:graphicImage>
与数据库中的动态资源:Display dynamic image from database with p:graphicImage and StreamedContent .在您的特定示例中,它看起来像这样:
同