我使用了一个fileDownloadActionListener来从ADF表单打开一个jasper报告。第一次,它正确打开。但第二次,它在应用程序上显示错误消息,“文件未下载或未正确下载。”
这是我的代码:
public void reportAction(FacesContext ctx,OutputStream output) throws FileNotFoundException,NamingException,
SQLException, IOException, JRException,
ClassNotFoundException,
InstantiationException,
IllegalAccessException,StreamCorruptedException {
File input = null;
Connection conn = null;
String branchId = getBranchId().getValue().toString();
Map reportParameters = new HashMap();
reportParameters.put("branchId", branchId);
bindings = this.getBindings();
ctx = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
ServletOutputStream servletOutputStream;
servletOutputStream = response.getOutputStream();
String reportPath = ctx.getExternalContext().getInitParameter("reportpath");
input = new File(reportPath+"testJob.jasper");
byte[] bytes= null;
if(bindings!=null){
OperationBinding ob = bindings.getOperationBinding("getCurrentConnection");
ob.execute();
conn = (Connection)ob.getResult();
System.out.println("Report Path===========>"+input.getPath().toString());
if(input.getPath()!=null&&reportParameters!=null&&conn!=null){
JRPdfExporter pdfExporter = new JRPdfExporter();
bytes = JasperRunManager.runReportToPdf(input.getPath(), reportParameters, conn);
JasperPrint print = JasperFillManager.fillReport(input.getPath(),reportParameters,conn);
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, input.getPath());
pdfExporter.exportReport();
response.addHeader("Content-disposition", "attachment;filename=testJob1.pdf");
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
File file = new File(input.getPath());
output = response.getOutputStream();
servletOutputStream.write(bytes, 0, bytes.length);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bui = new BufferedInputStream(fis);
int readBytes = 0;
while ((readBytes = bui.read()) != -1)
output.write(readBytes);
bui.close();
fis.close();
servletOutputStream.flush();
servletOutputStream.close();
ctx.responseComplete();
}
}
else{
ctx.addMessage(null,new FacesMessage("No bindings configured for this page"));
}
}