我正在使用 JasperReprots。这是我的代码的一部分:
ServletContext context = this.getServletConfig().getServletContext();
File reportF = new File(context.getRealPath(rF));
byte[] bytes = null;
ServletOutputStream servletOutputStream = resp.getOutputStream();
InputStream reportStream = new FileInputStream(reportF.getPath());
reportF.delete();
bytes = JasperRunManager.runReportToPdf(reportStream, new HashMap(),new JREmptyDataSource());
resp.setContentType("application/pdf");
resp.setContentLength(bytes.length);
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
在此之后,我可以在浏览器中看到 pdf,但是当我尝试保存它时,该文件没有扩展名 pdf。如何在不在我的服务器上保存报告的情况下添加此扩展程序?
最佳答案
这应该可以解决问题:
resp.setHeader("Content-Disposition", "attachment;filename=report.pdf");
关于java - JasperReports 中的可下载 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6979532/