How to insert images from database?

2020-01-27 08:35发布

问题:

I want insert images from database in details band in my report in Jaspersoft Studio.
The JPG-images are saved in MySQL in the field imgdata of type longblob.
I`v tried to put this expressions in Image element:

  1. $F{imgdata}
  2. MyUtil.getInputStream($F{imgdata})

In the first case I get this error:

net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRException: Unknown image source class [B

... and in second case this error:

net.sf.jasperreports.engine.JRException:
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error
evaluating expression :  Source text :
MyUtil.getInputStream($F{imgdata})

My question: How to insert images from database into JasperReports's report?

回答1:

Open the XML view of .jrxml file. Then find code similar to this

<field name="image" class="java.lang.Object"/>  
/* "image" must be replaced by your actual field name */

and change it to

<field name="image" class="java.io.InputStream"/>


回答2:

It seems that I solved the issue myself.

Here are the steps I`v done:

1) The expression for the Image report field should be: $F{imgdata}

(Note: imgdata is name of the database field where image data is stored)

2) Open the "Dataset and Query Dialog" of your report and change the Class Type of the imgdata field from java.lang.Object to java.io.InputStream (see the screenshot --- sorry I dont have enough reputation points to post images). Close the dialog.

3) Generate your report by switching to Preview-Tab. Now you should see the images in your report.