我使用以下Java代码从web应用程序下载文件:
@RequestMapping(value = "/filedownloads/filedownload/{userid}/{projectid}/{documentfileid}/{version}/", method = RequestMethod.GET)
public void filesDownload(final @PathVariable("userid") String userId, final @PathVariable("projectid") String projectId,
final @PathVariable("documentfileid") String documentFileId, final @PathVariable("version") String version,
final HttpServletResponse response) throws IOException, BusinessException {
...
final String fileName = "filename=" + documentFile.getFileName();
final InputStream is = new FileInputStream(filePath);
response.setHeader("Content-Disposition", "inline; " + fileName);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
}
如果我下载一个pptx文件,我会得到以下IE页面:
我想做的是在Powerpoint中打开下载的文件。我现在的问题是,是否有一个标题设置,以便使用正确的应用程序(在本例中为Powerpoint)打开此文件
发布于 2016-10-08 20:06:10
只需尝试正确设置Content Type
标头,如果是pptx
,则设置为application/vnd.openxmlformats-officedocument.presentationml.presentation
,如下所示:
response.setContentType(
"application/vnd.openxmlformats-officedocument.presentationml.presentation"
);
response.setHeader(
"Content-Disposition",
String.format("inline; filename=\"%s\"", documentFile.getFileName())
);
response.setContentLength((int) new File(filePath).length());
Here is the list of mime types corresponding to Office 2007 documents.
发布于 2016-10-12 05:45:39
下面是来自Spring MVC控制器的一小段示例代码:
@RequestMapping("/ppt")
public void downloadPpt(HttpServletRequest request, HttpServletResponse response) throws IOException {
Resource resource = new ClassPathResource("Presentation1.pptx");
InputStream resourceInputStream = resource.getInputStream();
response.setHeader("Content-Disposition", "attachment; filename=\"Presentation1.pptx\"");
response.setContentLengthLong(resource.contentLength());
byte[] buffer = new byte[1024];
int len;
while ((len = resourceInputStream.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, len);
}
}
通过将Content-Disposition
设置为attachment
,您将告诉浏览器将此文件作为附件下载,并通过提供带扩展名的正确文件名,您将告诉操作系统使用用户通常用来打开此类文件的任何应用程序。在这种情况下,它将是MS Power Point。
这样,您就可以不必确切地知道文件是用哪个版本的Power Point创建的。
发布于 2016-10-12 17:37:21
我已经在IE-11
中测试了代码,它工作正常。参见以下代码,即
@RequestMapping(value = "/downloadfile", method = RequestMethod.GET)
@ResponseBody
public void downloadfile(HttpServletRequest request, HttpServletResponse response) throws Exception {
ServletOutputStream servletOutputStream = null;
try {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=downloadppt.pptx");
byte[] ppt = downloadFile();
servletOutputStream = response.getOutputStream();
servletOutputStream.write(ppt);
} catch (Exception e) {
throw e;
} finally {
servletOutputStream.flush();
servletOutputStream.close();
}
}
从保存的pptx
文件生成bytes
。
public byte[] downloadFile() throws IOException {
InputStream inputStream = new FileInputStream(new File("e:/testppt.pptx"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// Transfer bytes from source to destination
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
byteArrayOutputStream.write(buf, 0, len);
}
inputStream.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toByteArray();
}
就这样,你可以下载pptx
文件了。希望代码能对你有所帮助,如果你有任何疑问或疑问,我们可以讨论或提出任何建议。谢谢
https://stackoverflow.com/questions/39828967
复制相似问题