正如我们从 javadoc 中看到的JRHtmlExporterParameter.IMAGES_URI http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/export/JRHtmlExporterParameter.html#IMAGES_URI参数确实已被弃用,并且HtmlExporterOutput.getImageHandler() http://jasperreports.sourceforge.net/api/net/sf/jasperreports/export/HtmlExporterOutput.html#getImageHandler()应该使用方法来代替它。
定义图片存储路径
我们可以使用的实现Html 资源处理程序 http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/export/HtmlResourceHandler.html接口,例如WebHtml资源处理程序 http://jasperreports.sourceforge.net/api/net/sf/jasperreports/web/util/WebHtmlResourceHandler.html.
使用示例:
JRExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(out);
output.setImageHandler(new WebHtmlResourceHandler("/reports/image?image={0}"));
exporter.setExporterOutput(output);
exporter.exportReport();
定义导出期间保存图像的位置
在...的帮助下文件 Html 资源处理程序 http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/export/FileHtmlResourceHandler.html处理程序我们可以设置生成图像的路径html
使用示例:
JRExporter exporter = new HtmlExporter();
// output file for generated html report
File file = new File(String.format("./out/%1$s_%2$s.html", report.getTemplateName(), new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())));
ExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(file);
// the folder for storing images. It will be subfolder with name starting like generated html and ended with postfix "_images"
File resourcesDir = new File(file.getParent(), file.getName() + "_images");
// argument ({0}) will be replaced with the real image name
String pathPattern = resourcesDir.getName() + "/{0}";
exporterOutput.setImageHandler(new FileHtmlResourceHandler(resourcesDir, pathPattern));
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
生成的文件和文件夹将是这样的:
.. [Folder]
image-test_20170504232649.html [File]
image-test_20170504232649.html_images [Folder]
img_0_0_0.png [File]
Notes:
使用样本Html 资源处理程序可以被找寻到here http://jasperreports.sourceforge.net/sample.reference/webapp/index.html#webapp