Show image as byte[] from database as graphic image in JSF page -
i have image content byte[]
database.
private byte[] image;
how can show byte array real graphic image in jsf page?
this not directly possible <h:graphicimage>
. can point url, not byte[]
nor inputstream
. basically, need make sure bytes directly available http response on given url, can use in <h:graphicimage>
(or plain html <img>
).
provided you're identifying image id so:
<h:graphicimage value="/image/#{somebean.imageid}" />
here's kickoff example of such servlet:
@webservlet("/image/*") public class imageservlet extends httpservlet { @ejb private imageservice service; @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { long id = long.valueof(request.getpathinfo().substring(1)); image image = service.find(id); response.setcontenttype(getservletcontext().getmimetype(image.getname())); response.setcontentlength(image.getbytes().length); response.getoutputstream().write(image.getbytes()); } }
a more advanced abstract template static resource servlet, supporting http caching, can found in this answer, along concrete example serving database.
if happen use jsf utility library omnifaces on jsf 2.2 + cdi environment, can instead use <o:graphicimage>
can used more intuitively.
<o:graphicimage value="#{imagebean.getbytes(somebean.imageid)}" />
@named @applicationscoped public class imagebean { @ejb private imageservice service; public byte[] getbytes(long imageid) { return service.getimagebytes(imageid); } }
Comments
Post a Comment