我在servlet中有一个进程,该进程创建一个.pdf文件并将其发送给客户端。但是,Adobe不会打开下载的文件(“打开此文档时出错。该文件已损坏,无法修复。”)。驻留在服务器上的原始创建的文件很好,Adobe没问题打开它。
我的代码:
private static void sendFile(HttpServletResponse response, String pdfPath) throws FileNotFoundException, IOException {
PrintWriter out = response.getWriter();
File f = new File(pdfPath);
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition", "attachment; filename=\"" + f.getName());
response.setContentLength((int) f.length());
response.setContentType("application/pdf");
FileInputStream fileInputStream = new FileInputStream(pdfPath);
int i;
while ((i = fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
}
答案 0 :(得分:3)