【问题标题】:Error loading a compiled jasper file inside .war file在 .war 文件中加载已编译的 jasper 文件时出错
【发布时间】:2019-01-16 02:17:28
【问题描述】:

我在尝试加载 .war 文件中的 .jasper subReport 时遇到问题。
这个想法是加载一个位于“parte1”文件夹内的子报表。

documentation 中说您不能通过相对路径来加载子报表文件...

您不能使用相对路径来定位子报表文件;也就是说,如果您在 c:\myreport\main_report.jasper 中有报告,则不能使用 ..\mysubreports\mysubreport.jasper 之类的表达式来引用子报告。

这是因为 JasperReports 不会在内存中保存它正在使用的 Jasper 文件的原始位置。考虑到 Jasper 对象不一定是从物理文件中加载的,这很有意义。

所以我正在尝试将完整路径传递给 .jasper 文件。
1.- 我使用@Autowired 获取上下文,然后将路径作为字符串完成...

parameters.put("SUBREPORT_DIR", contextPath + "WEB-INF" + File.separator + "report" + File.separator + "parte1" + File.separator);

2.- 将路径作为参数传递给 jasperReport

printFileName = JasperFillManager.fillReportToFile(getPath(sourceFileName), parameters, new JRBeanCollectionDataSource(lstDataSource));

3.- 给出加载子报表文件的路径

<subreport>
            <reportElement x="50" y="50" width="200" height="200" uuid="ced7076b-b6ba-4973-8e4c-cd289bd77c4e"/>
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{motivoReclamo})]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "ClsMotivoReclamoReportBean.jasper"]]></subreportExpression>
        </subreport>

但是,当尝试运行它时。它显示此错误消息...

2019-01-15 05:29:19 [http-nio-8081-exec-2] 错误 xxx.xxx.xx.report.PDFGenerator - net.sf.jasperreports.engine.JRException:在以下位置找不到资源: C:\Program Files\Apache Software Foundation\Tomcat 8.5\webapps\xxxxxxxxx\WEB-INF\report\parte1\ClsMotivoReclamoReportBean.jasper。

提前致谢。

更新

我正在使用 Spring Boot 并生成一个 .war 文件

我正在使用这个 maven 插件来编译报告并生成 .jasper 文件。
https://github.com/alexnederlof/Jasper-report-maven-plugin
然后我使用这个 maven 插件来复制报告中需要的一些图像...

<plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/generated-sources/report</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/src/main/jasperfiles</directory>
                                <filtering>true</filtering>
                                <excludes>
                                    <exclude>**/*.jrxml</exclude>
                                </excludes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

然后我使用这个 maven 插件将 jasper 文件复制到 .war 中

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${project.build.directory}/generated-sources/report</directory>
                        <targetPath>WEB-INF/classes/report</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

【问题讨论】:

    标签: java jasper-reports war


    【解决方案1】:

    您也可以将子报表定义为 InputStream:

    在您的 JRXML 中:

    <parameter name="MY_SUB_REPORT" class="java.io.InputStream" isForPrompting="false"/>
      ...
    </subreport>
      ...
      <subreportExpression><![CDATA[$P{MY_SUB_REPORT}]]></subreportExpression>
    </subreport>
    

    在您的 Java 类中:

    parameters.put("MY_SUB_REPORT", new ByteArrayInputStream(Files.readAllBytes(path)));
    

    【讨论】:

    • 现在抛出 net.sf.jasperreports.engine.JRException:从 InputStream 加载对象时出错。下面几行... 原因:java.io.StreamCorruptedException:无效的流标头:EFBFBDEF
    • 也许你的碧玉有些损坏了?您是否对打包到 .war 中的资源使用 Maven 过滤?可能这是相关的:stackoverflow.com/questions/24078820/…
    • 感谢您的回答,请查看我的更新。突然间,如果我将建议的 maven 代码放入该答案中,它会破坏我的所有代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    相关资源
    最近更新 更多