Trying to get a blob and make it into a streamed content. I get the bytes, they do get converted into ByteArrayInputStream and I am returning the StremedContent image, but i keep getting this:
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/ConfigEmployee] threw exception
java.io.IOException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:56)
... 21 more
line 56 has this: externalContext.setResponseContentType(streamedContent.getContentType());
I need to add that the streamed content is returned two times per Image object.
Image bean:
public Image(byte[] bytes,String name)
{
this.id=new MyDatabase().getLastId("image")+1;
this.name=name;
this.byteData=bytes;
InputStream is=new ByteArrayInputStream(bytes);
this.image = new DefaultStreamedContent(is,"image/png");
}
public StreamedContent getImage()
{
return image;
}
HTML code
<div class="dataTable">
<h:form id="imageList">
<p:dataTable var="img" value="#{imageView.images}" rowKey="#{img.id}" rows="10" lazy="true" paginator="true">
<p:column headerText="Image">
<p:graphicImage style="width:80px; height:80px" value="#{img.image}" />
</p:column>
<p:column width="200" headerText="Name">
<h:outputText value="#{img.name}" />
</p:column>
<p:column width="200" headerText="Edit">
<h:commandButton value="Enter" actionListener="#{imageView.convertImage(img)}" action="Image?faces-redirect=true" />
</p:column>
</p:dataTable>
</h:form>
</div>
Lazy load
@Override
public List<Image> load(int first, int pageSize, String sortField, SortOrder sortOrder,
Map<String, Object> filters)
{
db.openDatabase();
ResultSet rs = db.getImage();
List<Image> imgList = new ArrayList<Image>();
try
{
if (rs.last())
this.setRowCount(rs.getRow());
for (rs.absolute(first); rs.next() && first <= (first + pageSize); first++)
{
imgList.add(new Image(rs.getBytes("byteData"), rs.getString("name")));
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
db.closeDatabase();
}
return imgList;
}
Primefaces 5.1, JSF 2.2, Tomcat v7.0