我已经编写了通过http请求下载pdf文件的代码。
public void downloadDocument(HttpServletRequest request,
HttpServletResponse response, @PathVariable("id") String docId)
throws Exception {
HttpSession session = request.getSession(true);
int accountId = (Integer) session.getAttribute("ownerAccountId");
Map<String, String> docMap = DbInteractor.getUploadedDocsByDocId(
Integer.valueOf(docId), accountId);
String docName = docMap.get("name");
String typeName = docMap.get("type");
String[] fileName = docName.split("\\.(?=[^\\.]+$)");
typeName= typeName.replace(" ", "");
if (typeName.equals("CCD/CCR")) {
typeName = "CCDorCCR";
}
String filename = typeName + docId + "." + fileName[1];
System.out.println(filename);
FileInputStream fileInputStream = new FileInputStream(
Constants.DOCUMENTS_PATH + filename);
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-disposition", "attachment; filename="
+ docName);
OutputStream os = response.getOutputStream();
IOUtils.copy(fileInputStream, os);
os.flush();
os.close();
}
但是这个文件会自动下载到chrome。有没有办法强制chrome在查看器中打开这个文档(或者要求在chrome中打开对话框)
答案 0 :(得分:0)
您通过发送“Content-Disposition:attachment”强制浏览器下载。