解决JasperReport 导出 Html 时图片显示问题


JasperReport导出为Html时,图片显示不正常的问题,从项目一开始就困扰着我,今天终于解决了。太爽了!o(∩∩)o…

首先自己犯了一个错误,Html报表的图片输出没有使用net.sf.jasperreports.j2ee.servlets.ImageServlet,导致新打开的报表总是使用的之前报表的图片,且第一次输出报表时,图片不加载。

JasperReport导出Html报表的正确配置应该是

  1. 在web.xml配置net.sf.jaasperreports.j2ee.servlets.ImageServlet
  2. 设置JRHtmlExporterParameter.IMAGES_DIR_NAME,用于存放图片
  3. 导出报表时设置JRHtmlExporterParameter.IMAGES_URI变量,内容和web.xml中配置的ImageServlet的url-pattern一致
  4. 在Session中设置print对象
  5. 然后就可以执行导出了

下面是代码

String jasperFilePath = “WEB-INF/reports/test.jasper”;
// 获得报表模板文件对象
File reportFile = getApplicationContext().getResource(jasperFilePath).getFile();

JasperPrint print = JasperFillManager.fillReport(reportFile.getAbsolutePath(), map, dataSource.getConnection());

JRHtmlExporter exporter = new JRHtmlExporter();
// 组装图片请求URI,增加time参数,防止同一个报表的图片缓存。
String imageURI = request.getContextPath() + “/service/reportImages?time=” + System.currentTimeMillis() + “&image=” ;

exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageURI);
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);
exporter.setParameter(JRExporterParameter. JASPER_PRINT, print);

exporter.setParameter(JRExporterParameter. OUTPUT_WRITER, response.getWriter());
exporter.setParameter(JRHtmlExporterParameter.FLUSH_OUTPUT, Boolean.TRUE);

exporter.exportReport();

参考:Chart caching issues with JasperReports web output? A solution!

Advertisements

解决JasperReport 导出 Html 时图片显示问题》上有1条评论

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s