Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

java - File resolver in Jasper Reports 5.0.1

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After browsing the sources I found the solution.

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:

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);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...