Jasper Reports not displaying pdf on browser, show

2019-08-06 08:29发布

This is a snippet of my Java class code

Case 1

    request=ServletActionContext.getRequest();
            response = ServletActionContext.getResponse();
            if(response!=null)
                System.out.println("Response is not null");
            System.out.println("coming to this method");
            inputStream= this.getClass().getClassLoader().getResourceAsStream("com/ram/report/jasper/Report.jrxml");
            JasperReport jasperReport=JasperCompileManager.compileReport(inputStream);
            HashMap<String, Object> params = null;

            JasperPrint jp=JasperFillManager.fillReport(jasperReport, params,new JRBeanCollectionDataSource(list));
            JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, new File("").getAbsolutePath()+"/UserReport.pdf");
            exporter.exportReport();
            File file = new File(new File("").getAbsolutePath()+"/UserReport.pdf"); 

            inputStream  = new FileInputStream(file);
            response.flushBuffer();
            System.out.println("Made report");
            return "ajaxReturn";
        }

Case 2

request=ServletActionContext.getRequest();
                response = ServletActionContext.getResponse();
                if(response!=null)
                    System.out.println("Response is not null");
                System.out.println("coming to this method");
                inputStream= this.getClass().getClassLoader().getResourceAsStream("com/ram/report/jasper/Report.jrxml");
                JasperReport jasperReport=JasperCompileManager.compileReport(inputStream);
                HashMap<String, Object> params = null;

                JasperPrint jp=JasperFillManager.fillReport(jasperReport, params,new JRBeanCollectionDataSource(list));
                JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
                exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, new File("").getAbsolutePath()+"/UserReport.pdf");
                exporter.exportReport();
                File file = new File(new File("").getAbsolutePath()+"/UserReport.pdf"); 

                inputStream  = new FileInputStream(file);
                ServletOutputStream op = response.getOutputStream();
                response.setContentType("application/pdf");
                response.setHeader("Content-Disposition",
                        "application; filename=\"" + "UserReport.pdf");
                byte[] bbuf = new byte[50000];
                int length=0;
                while ((inputStream != null) && ((length = inputStream.read(bbuf)) != -1)) {
                    op.write(bbuf, 0, length);
                }
                op.flush();
                op.close(); 
                response.flushBuffer();
                System.out.println("Made report");
                return "ajaxReturn";
            }

This is my action

<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="contentDisposition">attachment;filename="UserReport.pdf"</param>
    <param name="bufferSize">1024</param>
    <param name="inputName">inputStream</param>

    </result>

The problem I am facing is that the pdf is not displayed on the browser at all despite showing no error on the Eclipse console.
After checking a few links, I thought of mirroring into an outputStream, so I did Case 2. However it gave the same identical result.Moreover, "Made report" is also being printed, meaning that the file has been created in both cases. So what exactly is the error here?

0条回答
登录 后发表回答