尝试获取blob并将其转换为流式内容。我得到了字节,它们确实被转换为ByteArrayInputStream,我正在返回StremedContent图像,但我一直得到这个:
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
第56行有:externalContext.setResponseContentType(streamedContent.getContentType());
我需要补充一点,每个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代码
<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>
延迟加载
@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
答案 0 :(得分:1)
bean的范围是什么,它应该是View或Session作用域,您必须更改访问bean的方式。对于动态创建的图像,正在发送两个请求。一个呈现HTML,呈现img
标记。比第二个,基于 src 属性触发。模型必须保留后续请求中的值。
所有人都在这里精美地解释Display dynamic image from database with p:graphicImage and StreamedContent