The param id is read past times the streamer too purpose to fetch the entity that stores the byte[] data.
1.) In the JSF page, allow us define a graphicImage factor similar this:
<p:graphicImage id="serviceImage" cache="false" value="#{customImageStreamer.image}" height="150" width="150"> <f:param name="id" value="#{myService.entity.id}"></f:param> </p:graphicImage>
2.) Let us define a generic icon streamer that nosotros tin sack extend.
populace abstract course of educational activity ImageStreamer<T extends BaseEntity> { populace StreamedContent getImage() throws IOException { FacesContext context = FacesContext.getCurrentInstance(); if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { // So, we're rendering the HTML. Return a stub StreamedContent hence // that it volition generate correct URL. homecoming novel DefaultStreamedContent(); } else { // So, browser is requesting the image. Return a existent // StreamedContent amongst the icon bytes. Long id = Long.parseLong(context.getExternalContext().getRequestParameterMap().get("id")); T obj = getPersistenceService().findById(id); if (getImageArr(obj) != null) { homecoming novel DefaultStreamedContent(new ByteArrayInputStream(getImageArr(obj))); } else { homecoming novel DefaultStreamedContent(); } } } populace StreamedContent getImage(T obj) throws IOException { // So, browser is requesting the image. Return a existent // StreamedContent amongst the icon bytes. if (getImageArr(obj) != null) { homecoming novel DefaultStreamedContent(new ByteArrayInputStream(getImageArr(obj))); } else { homecoming novel DefaultStreamedContent(); } } protected byte[] downloadUrl(URL toDownload) { ByteArrayOutputStream outputStream = novel ByteArrayOutputStream(); InputStream current = null; stimulate { byte[] chunk = novel byte[4096]; int bytesRead; current = toDownload.openStream(); spell ((bytesRead = stream.read(chunk)) > 0) { outputStream.write(chunk, 0, bytesRead); } } select grip of (IOException e) { homecoming null; } in conclusion { if (stream != null) { stimulate { stream.close(); } select grip of (IOException e) { homecoming null; } } } homecoming outputStream.toByteArray(); } populace abstract PersistenceServicegetPersistenceService(); populace abstract byte[] getImageArr(T obj); }
3.) The actual course of educational activity implementation that streams the image. Note that it must endure applicationScoped.
@Named @ApplicationScoped populace course of educational activity StudentImageStreamer extends ImageStreamer{ @Inject mortal StudentService studentService; @Override populace PersistenceService getPersistenceService() { homecoming studentService; } @Override populace byte[] getImageArr(Student obj) { homecoming obj.getImageAsByteArr(); } }
If you lot bring approximately other course of educational activity that needs streaming for primefaces graphic image, too then but practice approximately other course of educational activity that extends ImageStreamer.
References:
http://stackoverflow.com/questions/8207325/display-dynamic-image-from-database-with-pgraphicimage-and-streamedcontent
0 komentar:
Please comment if there are any that need to be asked.