px背景图显示红叉叉解决方案
问题描述:使用JasperReport生成Html报表后,数据显示正确px图片不显示,页面整个都是红叉叉
<a href="http://blog.51cto.com/attachment/201208/142819880.jpg" target="_blank"></a>
产生原因:在生成HTML页面时JRHtmlExporterParameter.IMAGES_URI参数设置不正确,主要是图片的路径问题。由JasperReport生成Html时,那些红叉叉是一个名称为px像素的图片,是内置在jasperreports包中的,在IE显示时以图片的形式显示
解决办法:根据JasperReport的源代码显示,我们只需要以下两个步骤就能解决了
1、在web.xml中添加如下配置:
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<url-pattern>/servlets/image</url-pattern>
</servlet-mapping>
2、在调用程序中增加如下代码:
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
好了,经过上面两个步骤。重启服务,刷新页面立马就正常了
完整代码:
/**
* 导出html
*/
private static void exportHtml(JasperPrint jasperPrint,String defaultFilename,
HttpServletRequest request, HttpServletResponse response) throws IOException, JRException {
response.setContentType("text/html;charset=UTF-8");
JRHtmlExporter exporter = new JRHtmlExporter();
PrintWriter out = response.getWriter();
try {
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
exporter.exportReport();
} catch (JRException e) {
logger.debug(" 生成html文件失败 .... ....");
}
}
上面的代码就能很完美的生成html了,页面没有红叉叉,chat图也能正常显示了
本文转自 沫沫金 51CTO博客,原文链接:http://blog.51cto.com/zl0828/969891,如需转载请自行联系原作者