使用Struts2框架获取JasperReports生成的pdf以在浏览器上显示

时间:2015-12-23 12:58:00

标签: java pdf struts2 jasper-reports

我正在使用Jasper Reports框架以及我的Struts2 Web应用程序。只是在某个时候,我意识到 JasperViewer 无法帮助我 在客户端浏览器中显示和下载 我的报告。我看到了几个链接,几乎所有人都告诉我使用Servlets和OutputStreams来做这个。我尝试this链接获取Servelet请求和响应我的Java类。我对此非常,因此使用代码从这些来源this等执行此操作的各种尝试都不起作用。这就是为什么我要发布这个来寻求帮助。

我的Action类方法的相关代码是

public String execute() throws Exception //the method which will be executed
{
                ...
                request=ServletActionContext.getRequest();
                response = ServletActionContext.getResponse();
                if(response!=null)
                    System.out.println("Response is not null");
                System.out.println("coming to this method");
                java.io.InputStream in = this.getClass().getClassLoader().getResourceAsStream("com/ram/report/jasper/Report.jrxml");
                JasperReport jasperReport=JasperCompileManager.compileReport(in);
                HashMap<String, Object> params = new HashMap<String, Object>();             
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params,new JRBeanCollectionDataSource(list));
                JRPdfExporter pdfExporter = new JRPdfExporter();
                List<JasperPrint> jasperPrintList= new ArrayList<JasperPrint>(); 
                jasperPrintList.add(jasperPrint);
                pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,jasperPrintList);
                pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME ,request.getRealPath("")+"/Report.pdf");
                pdfExporter.exportReport();

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return "ajaxReturn";
        }
        }
            return "success";
    }   

我的操作(struts.xml与之链接)

<action name="reportData" class="Reportfetchnew">
        <interceptor-ref name="cookie"></interceptor-ref>
         <interceptor-ref name="cookieProvider"></interceptor-ref>
         <interceptor-ref name="defaultStack"></interceptor-ref>
        <result name="success">htdocs/jsp/report_new/report_tool.jsp</result>
         <result name="ajaxReturn" type="stream" >
            <param name="contentType">application/pdf</param>
            <param name="inputName">in</param>
            <param name="bufferSize">1024</param>

            </result>

        </action>

我收到此错误

Exception occurred during processing request: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.

问题在于,在此之前,当我使用JasperViewer查看我的Jasper文件时,它运行正常。 我该怎么做才能解决此错误让我的pdf可供下载?

0 个答案:

没有答案