无法从 ByteArrayInputStream 转换为 String 尝试在 jasper 报告中加载图像

问题描述 投票:0回答:1

我正在尝试通过表达式在我的报告中加载图像,但我收到错误:

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 ByteArrayInputStream((byte[]) ((java.lang.Object)parameter_logobrasao.getValue()))); //$JR_EXPR_ID=25$
                        <-------------------------------------------------------------------------------------------------------->
2. Cannot cast from ByteArrayInputStream to String
                value = (java.lang.String)(new ByteArrayInputStream((byte[]) ((java.lang.Object)parameter_logobrasao.getValue()))); //$JR_EXPR_ID=25$
                        <-------------------------------------------------------------------------------------------------------->
3. Cannot cast from ByteArrayInputStream to String
                value = (java.lang.String)(new ByteArrayInputStream((byte[]) ((java.lang.Object)parameter_logobrasao.getValue()))); //$JR_EXPR_ID=25$
                        <-------------------------------------------------------------------------------------------------------->

通过类型为 Object 的参数传递徽标,并在表达式处转换为 byte[]:

<parameter name="logobrasao" class="java.lang.Object"/>
....

<image hAlign="Center" vAlign="Middle">
   <reportElement x="0" y="20" width="557" height="150"/>
   <imageExpression><![CDATA[new ByteArrayInputStream((byte[]) $P{logobrasao})]]></imageExpression>
</image>

我正在用java拍摄图像:

...
logobrasao = getImageBytes();
                
parameters.put("logobrasao", logobrasao);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameter, beanDS);
...

Jasper 试图以某种方式自动将表达式转换为 String。我该如何解决?

版本:

  • Jaspersoft Studio 6.21.3
  • jasperreports-3.7.6
java image jasper-reports
1个回答
0
投票

您可能需要指定图像表达式的预期类型(注意

class
属性):

<image hAlign="Center" vAlign="Middle">
   <reportElement x="0" y="20" width="557" height="150"/>
   <imageExpression class="java.io.InputStream"><![CDATA[new ByteArrayInputStream((byte[]) $P{logobrasao})]]></imageExpression>
</image>

JasperReports 3.7.6 已经 14 岁了。 不再需要设置表达式的预期类型,因此您可以考虑升级到更新的版本。

© www.soinside.com 2019 - 2024. All rights reserved.