Java JasperReports中的可下载pdf
我和JasperReprots一起工作。这是我代码的一部分:Java JasperReports中的可下载pdf,java,web-applications,jasper-reports,Java,Web Applications,Jasper Reports,我和JasperReprots一起工作。这是我代码的一部分: ServletContext context = this.getServletConfig().getServletContext(); File reportF = new File(context.getRealPath(rF)); byte[] bytes = null; ServletOutputStream servletOut
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。如何在不将报表保存到我的服务器上的情况下添加此扩展?这可能会起到以下作用:
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();
resp.setHeader("Content-Disposition", "attachment;filename=report.pdf");
好啊我在我的代码中做了一些更改,并且我使用了您的建议,而且效果很好。我认为你的代码行有错误。它应该是:resp.setHeader(“内容处置”、“附件;文件名=\”report.pdf\”);很高兴听到它起作用了。虽然这很奇怪,但我在答案中使用了代码,而且效果很好。