使用 PostgreSql 在 Ireports 中显示图像
我正在尝试将图像从 PostgreSQL 数据库加载到 jaspersoft ireports(4.0) 中,但收到错误。
在 PostgreSQL 中,图像存储为 bytea 对象。在 ireports 中,我已将图像字段的属性更改为 java.io.InputStream。
将图像放入报告后,设置以下属性:
表达式类:java.io.InputStream
图像表达式:图像字段
我也尝试了此教程用于在 ireport 中显示 blob 图像。
显示以下错误:
Error filling print... Image read failed.
Setting up the file resolver...
net.sf.jasperreports.engine.JRException: Image read failed.
at net.sf.jasperreports.engine.util.JRJdk14ImageReader.readImage(JRJdk14ImageReader.java:73)
at net.sf.jasperreports.engine.util.JRImageLoader.loadImage(JRImageLoader.java:245)
at net.sf.jasperreports.engine.JRImageRenderer.getImage(JRImageRenderer.java:476)
at net.sf.jasperreports.engine.JRImageRenderer.getDimension(JRImageRenderer.java:512)
at net.sf.jasperreports.engine.fill.JRFillImage.fitImage(JRFillImage.java:1251)
at net.sf.jasperreports.engine.fill.JRFillImage.prepare(JRFillImage.java:1173)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:329)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:419)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:378)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2038)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:760)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:270)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:877)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Print not filled. Try to use an EmptyDataSource...
I am trying to load image from PostgreSQL database into jaspersoft ireports(4.0) but I am receiving an error.
In PostgreSQL image is stored as bytea object. In ireports I have changed the property of image field to java.io.InputStream
.
After placing the image in report following properties were set:
Expression class: java.io.InputStream
Image expression: image field
I also tried this tutorial to show blob images in ireport.
Following error is displayed:
Error filling print... Image read failed.
Setting up the file resolver...
net.sf.jasperreports.engine.JRException: Image read failed.
at net.sf.jasperreports.engine.util.JRJdk14ImageReader.readImage(JRJdk14ImageReader.java:73)
at net.sf.jasperreports.engine.util.JRImageLoader.loadImage(JRImageLoader.java:245)
at net.sf.jasperreports.engine.JRImageRenderer.getImage(JRImageRenderer.java:476)
at net.sf.jasperreports.engine.JRImageRenderer.getDimension(JRImageRenderer.java:512)
at net.sf.jasperreports.engine.fill.JRFillImage.fitImage(JRFillImage.java:1251)
at net.sf.jasperreports.engine.fill.JRFillImage.prepare(JRFillImage.java:1173)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:329)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:419)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:378)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2038)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:760)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:270)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:877)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Print not filled. Try to use an EmptyDataSource...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 PostgreSQL 中,
bytea
类型不是 BLOB,并且不能使用流。在 SQL 标准术语中,它是一个字节字符串。它应该被加载到 Java 中的字节数组中。如果您想在 PostgreSQL 中使用 BLOB 技术,则必须使用大对象功能。
http://www.postgresql.org/docs/current/interactive/largeobjects.html
In PostgreSQL the
bytea
type is not a BLOB, and you can't use a stream. In SQL standard terms it is a byte string. It should be loaded to a byte array in Java.If you want to use BLOB techniques in PostgreSQL you must use the large objects feature.
http://www.postgresql.org/docs/current/interactive/largeobjects.html
花了将近一天的时间,终于找到了解决办法;
选择convert(your_image,'base64') as img from yourtable
java.io.InputStream
net.sf中将 。 jasperreports.engine.util.JRImageLoader.getInstance(new SimpleJasperReportsContext()).loadAwtImageFromBytes(javax.xml.bind.DatatypeConverter.parseBase64Binary($F{img}))
编辑:
我的 Postgresql 版本是:9.4 ,
感谢 FiruzzZ,他指出在 postgresql 9.1 中代替了转换函数,编码(bytea,'base64')可用。
spending nearly one day, finally found the solution;
select convert(your_image,'base64') as img from yourtable
java.io.InputStream
net.sf.jasperreports.engine.util.JRImageLoader.getInstance(new SimpleJasperReportsContext()).loadAwtImageFromBytes(javax.xml.bind.DatatypeConverter.parseBase64Binary($F{img}))
EDIT:
My Postgresql version is : 9.4,
Thanks to FiruzzZ he stated that in postgresql 9.1 instead of convert function, encode(bytea,'base64') is available.
将 blob (postgresql) 中的图像显示到 iReport 中的图像
在 Postgresql 中,图像存储为 blob-oid
ReportController 中的代码
Displaying Image from blob (postgresql) to Image in iReport
In Postgresql image is stored as blob-oid
Code inside ReportController