Jasper报告不显示图像

时间:2013-07-17 08:05:47

标签: java jasper-reports freemarker html-rendering

首先,抱歉我的英语不好。我的问题是: 我尝试在html页面上显示报告预览。为此,设置参数:

request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFilePath);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/preview/image?image=");
exporter.exportReport();

可以看出,我们将报告导出到指定目录中的html文件中。在网络上,我添加了以下内容:

<servlet>
    <servlet-name>ImageServlet</servlet-name>
    <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ImageServlet</servlet-name>
    <url-pattern>/preview/image</url-pattern>
</servlet-mapping>

为了显示html,我们有ftl控制器:

 @RequestMapping(value = "/events/preview", method = RequestMethod.POST)
 public void reportPreviewEvents(String requestInJson,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam("pageNo") Integer pageNo,
                                HttpServletResponse response,
                                HttpServletRequest request,
                                Model model) {
    try {
        Map<String, Object> toModel = auditHandler.handleExportEvents(requestInJson, JasperExporter.Type.HTML, pageSize, pageNo, request);
        model.addAllAttributes(toModel);
        String filePath = (String) toModel.get("filePath");
        reportContent = org.apache.commons.io.IOUtils.toString(new FileInputStream(filePath), Charset.forName("UTF-8"));
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(reportContent);

但是当我打开它(ip:port / context / report / events / preview /)时,我无法在html中看到图像。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

这是你可以生成HTML报告并指向更新的JasperReports库的图像servlet的方式(我使用的是6.1.0),因为许多旧函数已被弃用:

StringBuffer sbuffer = new StringBuffer();        
HtmlExporter exporterHTML = new HtmlExporter();
SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
exporterHTML.setExporterInput(exporterInput);

SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(sbuffer);
//pointing to the image servlet
HtmlResourceHandler imageHandler = new WebHtmlResourceHandler( request.getContextPath() + "/servlets/image?" + ImageServlet.IMAGE_NAME_REQUEST_PARAMETER
        + "={0}" + "&uuid=" + UUID.randomUUID().toString());
exporterOutput.setImageHandler(imageHandler);

//setting some sonfigs for the report
SimpleHtmlExporterConfiguration htmlConfig = new SimpleHtmlExporterConfiguration();
SimpleHtmlReportConfiguration htmlReportConfiguration = new SimpleHtmlReportConfiguration();
htmlConfig.setHtmlFooter("");
htmlConfig.setHtmlHeader("");
htmlConfig.setBetweenPagesHtml("");
htmlReportConfiguration.setPageIndex(pageIndex);

exporterHTML.setConfiguration(htmlConfig);
exporterHTML.setExporterOutput(exporterOutput);
exporterHTML.setConfiguration(htmlReportConfiguration);

exporterHTML.exportReport();

希望有人觉得它有用。