使用5.0.1的REPORT_FILE_RESOLVER已过时,示例实现为:
http://jasperreports.sourceforge.net/sample.reference/tableofcontents/index.html#fileresolver
说明强烈建议切换到JasperReportsContext。
我找不到JasperReportsContext用法的任何示例。据我所知,我应该寻找具有FileResolver getter和setter的LocalJasperReportsContext。
我在问,该怎么做?
最佳答案
浏览源代码后,我找到了解决方案。
JRXML imageExpression标签:
<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代码段:
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。