Itext pdf下载后无效

时间:2013-12-03 21:15:50

标签: java servlets itext

当我下载签名的pdf文件时,它被Adobe Reader显示为无效。我可以在我的本地项目文件夹中的服务器上正常查看此文件,但是当我通过我的servlet下载该文件的过程时,在此下载过程中的某些内容使得pdf无效。 这是我的servlet下载代码:

response.setContentType("application/pdf");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-Disposition","inline;filename=CompensationReport" + compensation+".pdf");
response.setHeader("Accept-Ranges", "bytes");

FileInputStream fis = new FileInputStream(thePdf1);
BufferedInputStream bis = new BufferedInputStream(fis);
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[2048];
while (true) {
    int bytesRead = bis.read(buffer, 0, buffer.length);
    if (bytesRead < 0) {
        break;
    }
    sos.write(buffer, 0, bytesRead);
    sos.flush();
}
sos.flush();
bis.close();  
return;

1 个答案:

答案 0 :(得分:3)

如上所述here尝试设置Content-Length

response.setContentLength(filebytesize);