本文介绍了Jasper Reports 5.0.1中的文件解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

5.0.1不推荐使用REPORT_FILE_RESOLVER和示例实现:

with 5.0.1 the REPORT_FILE_RESOLVER was deprecated and the sample implementations:http://jasperreports.sourceforge.net/sample.reference/tableofcontents/index.html#fileresolver

表示强烈建议切换到JasperReportsContext。

state that it's highly recommended to switch to JasperReportsContext.

我找不到任何JasperReportsContext用法的例子。据我所知,我应该寻找具有FileResolver getter和setter的LocalJasperReportsContext。

I couldn't find any examples of JasperReportsContext usage. As far as I know I should be looking for LocalJasperReportsContext which has a FileResolver getter and setter.

我在问,它是如何完成的?

I'm asking, how does it have to be done?

推荐答案

浏览完资源后,我找到了解决方案。

After browsing the sources I found the solution.

JRXML imageExpression标记:

The JRXML imageExpression tag:

<band height="79" splitType="Stretch">
<image scaleImage="FillFrame" isLazy="true" onErrorType="Blank">
    <reportElement uuid="3340bf0f-8471-45e9-8ea4-bdf44a7c0e68" x="0" y="0" width="150" height="69"/>
    <imageExpression class="java.io.File"><![CDATA["image.jpg"]]></imageExpression>
</image>

Java code snippet:

Java code snippet:

FileResolver resolver = new FileResolver() {
@Override
public File resolveFile(String filename) {
    return new File("/some/path");
}
};


InputStream jasperfile = getClass().getClassLoader().getResourceAsStream("file.jasper");

LocalJasperReportsContext ctx = new LocalJasperReportsContext(DefaultJasperReportsContext.getInstance());
ctx.setClassLoader(getClass().getClassLoader());
ctx.setFileResolver(resolver);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile);

JasperFillManager fillmgr = JasperFillManager.getInstance(ctx);
JasperExportManager exmgr = JasperExportManager.getInstance(ctx);

JasperPrint jasperPrint = fillmgr.fill(jasperReport, parameters, beanColDataSource);
ByteArrayOutputStream pdfBytes = new ByteArrayOutputStream();
exmgr.exportToPdfStream(jasperPrint, pdfBytes);

您必须创建一个新的上下文并将其传递给JasperFillManager和JasperExportManager。

You have to create a new context and pass it to JasperFillManager and JasperExportManager.

这篇关于Jasper Reports 5.0.1中的文件解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 22:46