导出html页面时,图像未加载到jasper报告中

时间:2014-12-17 12:26:39

标签: java jasper-reports

我在使用jasper报告导出html时无法显示图像。我试图从这个论坛找到不同的解决方案。但我没有任何幸运,我需要在哪里可以错过。我是分享我的代码

jasper代码:

    <parameter name="IMG_PATH" class="java.lang.String">
        <parameterDescription><![CDATA[]]></parameterDescription>
    </parameter>


<image>
<reportElement uuid="f4c3be1d-ec89-4e22-92c5-e75fbad2bcc5" isPrintRepeatedValues="false" x="567" y="2" width="174" height="56"/>
    <imageExpression><![CDATA[$P{IMG_PATH}+"\\logo.gif"]]></imageExpression>
</image>

Java代码: -

    rptExporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
    rptExporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
    rptExporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "UTF-8"); 
    request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);



    if (reportFrmType.equalsIgnoreCase("HTML")) {


                JRHtmlExporterParameter.CHARACTER_ENCODING, "UTF-8");
        rptExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
                "http://" + request.getServerName() + ":"
                        + request.getServerPort() + ""
                        + request.getContextPath()
                        + "/resources/images/logo.png");

        rptExporter.setParameter(
                JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR,
                Boolean.TRUE);

        rptExporter.setParameter(
                JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
                Boolean.FALSE);


    }

但我无法获取图片并看到Google Chrome&gt;控制台和获取图像src像这样

<tr valign="top">
  <td colspan="2" style="width: 292px; height: 43px;"></td>
  <td style="width: 4px; height: 43px;"></td>
  <td colspan="2" rowspan="4"><img src="http://localhost:7005/SamplePortal/resources/images/logo.pngimg_0_0_5" style="width: 174px" alt=""></td>
</tr>

当我删除&#34; img_0_0_5&#34;从Googlechrome控制台然后加载image.why附加&#34; img_0_0_5&#34;在src?

我尝试了另一种方式,即使我无法获取图像并在Chrome浏览器中检查控制台

<td colspan="2" rowspan="4"><img src="image?image=img_0_0_5" style="width: 174px" alt=""></td>

下面的共享代码

web.xml中: -

 <servlet>  
    <servlet-name>ImageServlet</servlet-name>  
    <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>  
  </servlet>  

Java代码: -

request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
         Map imagesMap= new HashMap();  
         imagesMap.put("IMG_PATH", "wda-logo.png");
         request.getSession().setAttribute("IMAGES_MAP", imagesMap);

        if (reportFrmType.equalsIgnoreCase("HTML")) {

            rptExporter.setParameter(
                    JRHtmlExporterParameter.CHARACTER_ENCODING, "UTF-8");


             rptExporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP,
                    imagesMap);
             rptExporter.setParameter(
                      JRHtmlExporterParameter.IMAGES_URI, 
                      "image?image=");
            rptExporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME,
                    "test");
            rptExporter.setParameter(
                    JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR,
                    Boolean.TRUE);

            rptExporter.setParameter(
                    JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,


        }

1 个答案:

答案 0 :(得分:0)

我错过了IMAGES_URI中的上下文路径,并在servlet映射中给出了错误。我分享了下面的代码

if (reportFrmType.equalsIgnoreCase("HTML")) {

            rptExporter.setParameter(
                    JRHtmlExporterParameter.CHARACTER_ENCODING, "UTF-8");



              rptExporter.setParameter(
                      JRHtmlExporterParameter.IMAGES_URI, 
                      "http://" + request.getServerName() + ":"
                        + request.getServerPort() + ""
                        + request.getContextPath()
                        + "/servlets/image?image=");

           .....


        }

和web.xml

<servlet-name>ImageServlet</servlet-name>  
    <url-pattern>/servlets/image</url-pattern>  
    </servlet-mapping> 

谢谢大家的支持。