Jasper Reports not displaying pdf on browser, shows no error on console

不羁的心 提交于 2019-12-11 12:25:31

问题


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?

来源:https://stackoverflow.com/questions/34438657/jasper-reports-not-displaying-pdf-on-browser-shows-no-error-on-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!