祝你有个愉快的一天。我正在使用Seam 2,丰富的面孔和主要面,Hibernate JPA和PostgreSQL。以前我已经为一些带有图像字段的表做了一些目录。我使用这些注释来声明表格中的图像字段:
@Lob
@Basic(fetch=FetchType.LAZY)
@Column(name = "fotografia")
public byte[] getFotografia() {
return fotografia;
}
public void setFotografia(byte[] fotografia) {
this.fotografia = fotografia;
}
它存储在PostgreSQL数据库中我的表中的oid列中。之后,我可以使用primefaces标签显示图像,在我的视图中显示没有问题的图像:
<p:graphicImage id="_ftFur"
value="#{genericImageStreamer.image}"
rendered="#{genericImageStreamer.image!=null}"
alt="Sin fotografia" style="width:320px; height:420px;"
styleClass="ui-image-container" />
所以它在我看来没有问题。但现在我需要创建一个PDF报告,显示我的表中的行列表,我需要显示我为每行存储的图像,但它不起作用。我使用seam itext。
对于我的代码,在我用来遍历ArrayList的repeater标签内部,我正在尝试使用p:image标签和属性imageData,如文档中所示:
<ui:fragment rendered="#{vis.image != null}">
<p:image alignment="center" imageData="#{vis.image}" />
</ui:fragment>
但是在我尝试生成报告时总是会出错:
10:06:28,006 ERROR [STDERR] 04-14-2014 10:06:28 AM com.sun.facelets.FaceletViewHandler handleRenderException
GRAVE: Error Rendering View[/inv/producto/repVestidosPDF.xhtml]
java.lang.NullPointerException
at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:89)
at com.lowagie.text.Image.getInstance(Unknown Source)
at org.jboss.seam.pdf.ui.UIImage.createITextObject(UIImage.java:145)
at org.jboss.seam.pdf.ui.ITextComponent.encodeBegin(ITextComponent.java:213)
at org.jboss.seam.pdf.ui.ITextComponent.encode(ITextComponent.java:283)
at org.jboss.seam.pdf.ui.ITextComponent.encode(ITextComponent.java:295)
at org.jboss.seam.pdf.ui.ITextComponent.encodeChildren(ITextComponent.java:256)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
at com.sun.facelets.component.RepeatRenderer.encodeChildren(RepeatRenderer.java:50)
at com.sun.facelets.component.UIRepeat.process(UIRepeat.java:357)
at com.sun.facelets.component.UIRepeat.encodeChildren(UIRepeat.java:617)
at org.jboss.seam.pdf.ui.ITextComponent.encode(ITextComponent.java:289)
at org.jboss.seam.pdf.ui.ITextComponent.encodeChildren(ITextComponent.java:256)
at org.jboss.seam.pdf.ui.ITextComponent.encode(ITextComponent.java:289)
at org.jboss.seam.pdf.ui.ITextComponent.encodeChildren(ITextComponent.java:256)
at org.jboss.seam.pdf.ui.ITextComponent.encode(ITextComponent.java:289)
at org.jboss.seam.pdf.ui.ITextComponent.encodeChildren(ITextComponent.java:256)
at org.jboss.seam.pdf.ui.ITextComponent.encode(ITextComponent.java:289)
at org.jboss.seam.pdf.ui.ITextComponent.encodeChildren(ITextComponent.java:256)
at org.jboss.seam.pdf.ui.ITextComponent.encode(ITextComponent.java:289)
at org.jboss.seam.pdf.ui.ITextComponent.encodeChildren(ITextComponent.java:256)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:942)
at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:368)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:495)
at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:662)
根据接缝文件:
p:image inserts an image into the document. Images can be loaded
from the classpath or from the web application context using the value
attribute.
Resources can also be dynamically generated by application code.
The imageData attribute can specify a value binding expression whose
value is a java.awt.Image object.
这就是我正在做的事情,使用我从数据库中获取的byte []矩阵创建一个java.awt.Image对象。我已经尝试了这些代码行,获得了相同的错误消息:
//1
Toolkit toolkit = Toolkit.getDefaultToolkit();
productoIter.setImage(toolkit.createImage(img.get(0).getFotografia()));
//2
Image image = ImageIO.read(new ByteArrayInputStream(img.get(0).getFotografia()));
productoIter.setImage(image);
//3 genericImageStreamer.setMimeType(img.get(0).getMimeType());
genericImageStreamer.setInputStream(img.get(0).getFotografia());
Image image = ImageIO.read(genericImageStreamer.getImage().getStream());
productoIter.setImage(image);
productoIter是一个迭代器,用于设置java.awt.Image对象,从数据库中获取列表后,我遍历它并尝试将byte []转换为java.awt.Image,但是当byte []不为null时,它会给我以前的错误。我不知道是否必须将该byte []转换为另一个东西,然后再将其用作参数来创建Image对象或类似的东西。
问候。
答案 0 :(得分:0)
好吧,我回答自己,因为经过大量搜索,我找到了解决问题的方法。基本上是关于生成 ByteArrayInputStream 而不是java.awt.Image,然后在标记中使用 value 属性而不是 imageData 。
这是完整答案的链接(检查发布的那个人的回复,他像我一样回答自己)。如果存储图像的mimetype,可能可以自定义数据类型。