Displaying image in Ireports using PostgreSql

3502 picture 3502 · Dec 8, 2011 · Viewed 8.8k times · Source

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...

Answer

kgrittn picture kgrittn · Apr 27, 2012

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