【问题标题】:BLOB image not displayed in jasper report pdf碧玉报告pdf中未显示BLOB图像
【发布时间】:2013-07-23 07:49:14
【问题描述】:

我已在我的 oracle 数据库中将图像保存为 BLOB。我的模型类包含 byte[] 图像;对应于数据库中的 BLOB 字段。我必须将数据库中的所有图像导出为 PDF。在java中我使用了以下代码:




    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() );

我使用 iReport 创建 jrxml 文件

在我的 jrxml 中,我创建了一个字段作为图像,字段类类型为 java.io.InputStream

在我的图像中,我将图像表达式指定为 $F{image},并将图像类表达式指定为 java.awt.Image。

我无法制作我的 pdf 报告。

我遇到了一个例外




    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)

我需要pdf格式的图片。

【问题讨论】:

    标签: jasper-reports


    【解决方案1】:

    错误消息“[B cannot be cast to java.io.InputStream”表示字节数组([B)无法转换为 InputStream。问题是您的 jrxml 文件中的图像字段类型与您的 ImageObj bean 中的字段类型不匹配。

    根据 JasperReports Ultimate Guide,您可以使用以下类型作为图像表达式的输入数据:

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

    因此,您必须将 byte[] 转换为另一种数据类型。 InputStream 是最简单的方法:

    将 ImageObj 中的 getter 更改为:

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

    并将图像类表达式设置为 java.io.InputStream。

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多