Tom Sebastian
Tom Sebastian

Reputation: 3433

BLOB image not displayed in jasper report pdf

I have saved image in my oracle data base as BLOB. My model class contains byte[] image; corresponding to the BLOB feild in database.I have to export all the images in data base to PDF. In java i used the following code:




    JasperReport jasperReport = JasperCompileManager.compileReport('jrxml file');
       JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(imageObjList);  
       //imageObjList containing the model 'ImageObj' which contain  byte[] image 
       JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter,ds);
       JasperExportManager.exportReportToPdfStream(jasperPrint,responce.getOutputStream() );

I used iReport for creating jrxml file

In my jrxml i create a field as image with field class type as java.io.InputStream

and in my Image i give Image Expression as $F{image} and also have given Image Class expression as java.awt.Image.

I am not able to make my pdf report .

I am getting an exception as




    net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression: 
       Source text : $F{image}
       ..... 
       Caused by: java.lang.ClassCastException: [B cannot be cast to java.io.InputStream
       at ImageReport_1374240048064_891215.evaluate(ImageReport_1374240048064_891215:171)

I need the images in pdf.

Upvotes: 2

Views: 4748

Answers (1)

Markus Kreusch
Markus Kreusch

Reputation: 2121

The error message "[B cannot be cast to java.io.InputStream" means that a byte-array ([B) can not be cast to InputStream. The problem is that the type of the image field in your jrxml file does not match the type of the field in your ImageObj bean.

According to the JasperReports Ultimate Guide you can use the following types as input data for an image expression:

  • java.lang.String
  • java.io.File
  • java.net.URL
  • java.io.InputStream
  • java.awt.Image
  • net.sf.jasperreports.engine.JRRenderable

So you will have to convert your byte[] to another datatype. InputStream is the easiest way to go:

Change the getter in ImageObj to:

public InputStream getImage() {
    return new ByteArrayInputStream(image);
}

and set the image class expression to java.io.InputStream.

Upvotes: 4

Related Questions