【发布时间】:2018-07-18 20:20:40
【问题描述】:
我在 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();
}
【问题讨论】:
-
警告:您的 Content-Disposition 有一个文件名的开始双引号,但没有结束双引号。
-
@VGR 不错。谢谢。