这是我的Java类代码的片段
案例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";
}
案例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";
}
这是我的行动
<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>
我面临的问题是,尽管在Eclipse控制台上没有显示任何错误,但pdf并未在浏览器上显示。
检查几个链接后,我想到镜像到一个outputStream,所以我做了案例2.但是它给出了相同的结果。另外,“Made report”也正在打印,这意味着该文件已在两种情况下创建。那么这里的错误到底是什么?