打开java web Application中生成的Pdf文件

时间:2013-08-12 12:39:18

标签: java-ee pdf

我正在我的网络应用程序中成功生成报告。我需要这个文件在客户端可用,我已经这样做了:

 String serverHomeDir = System.getenv("CATALINA_HOME");
        String reportDestination = serverHomeDir + "/Reports/" + user + "_" + church + "_" + currdate + ".pdf";
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=" + reportDestination);

目录上可用的Pdf文件可以成功打开。但是,无法打开客户端上的One下载。它给出了这个错误:

enter image description here

我在做什么错误或不做?

1 个答案:

答案 0 :(得分:1)

您必须将文件作为字节数组附加到响应中:

 String serverHomeDir = System.getenv("CATALINA_HOME");
 String reportDestination = serverHomeDir + "/Reports/" + user + "_" + church + "_" + currdate + ".pdf";

 FileInputStream fis = new FileInputStream(new File(reportDestination));

 // Fast way to copy a bytearray from InputStream to OutputStream
 org.apache.commons.io.IOUtils.copy(fis, response.getOutputStream());
 response.setContentType("application/pdf");
 response.setHeader("Content-Disposition", "attachment; filename=" + reportDestination);
 response.flushBuffer();