Jasper报告中的表达式值:“无法从ByteArrayInputStream转换为String”错误

时间:2017-08-16 10:11:52

标签: java xml jasper-reports

我正在尝试将xml中的编码标签转换为图像,然后使用Java和 Jaspersoft Studio 转换为PDF。

注意:当我在Jasper studio中预览时,我得到了正确的显示图像,但在使用Java main方法运行时遇到上述错误。 代码的动机是从xml获取标记并将其转换为带有图像的pdf。

我正在使用 JasperReports 6.4.0 。从 Jaspersoft Studio 图像预览时,正在正确显示jrxml文件并进行编译。但是在Java中使用main方法运行时,我遇到了错误。

以下是我正在使用的jrxml文件。

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="ImageDS" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <queryString language="xPath">
        <![CDATA[/Data/ListOfIdeaCafEntryFormIio/IdeaCafEntryFormBc]]>
    </queryString>
    <field name="Photo" class="java.lang.String">
        <fieldDescription><![CDATA[Photo]]></fieldDescription>
    </field>
    <group name="Photo">
        <groupExpression><![CDATA[$F{Photo}]]></groupExpression>
    </group>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="206" y="0" width="100" height="30"/>
                <text><![CDATA[Photo]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <image>
                <reportElement x="205" y="52" width="50" height="50"/>
                <imageExpression><![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($F{Photo}.getBytes()))]]></imageExpression>
            </image>
        </band>
    </detail>
</jasperReport>

用于生成PDF的Java代码:

public class Java8Base64Image {

    public static void main(String[] args) {
        BaseDao dao = new BaseDao();

        Connection connection = dao.getConnection();
        try {
            String reportName = "C:/..../MyReports/ImageDS";
            Map<String, Object> parameters = new HashMap<String, Object>();

            JasperCompileManager.compileReportToFile(reportName + ".jrxml");
            JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper", parameters, connection);
            // exports report to pdf
            JRExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(reportName + ".pdf"));
            exporter.exportReport();
        } catch (Exception e) {
            throw new RuntimeException("It's not possible to generate the pdf report.", e);
        } finally {
            if (connection != null) {
                try { connection.close(); } catch (Exception e) {}
            }
        }
    }
}

XML with image:

<Photo>/9j/..../OiiiQon/9k=</Photo>

我得到的错误

    Error is : Caused by: net.sf.jasperreports.engine.JRException: 
Errors were encountered when compiling report expressions class file: 
1. Cannot cast from ByteArrayInputStream to String value = (java.lang.String)(new java.io.ByteArrayInputStream(org.apache.commons.codec.binary‌​.Base64.decodeBase64‌​(((java.lang.String)‌​field_Photo.getValue‌​()).getBytes()))); //$JR_EXPR_ID=11$

0 个答案:

没有答案