Spring OutputStream - 用IE下载pptx

时间:2016-10-03 09:59:00

标签: java spring-mvc stream java-8

我使用此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页面:

opend pptx in IE

我想要做的是在Powerpoint中打开下载的文件。 我现在的问题是,如果有一个标题设置,以便使用正确的应用程序打开此文件(在本例中为Powerpoint)

3 个答案:

答案 0 :(得分:2)

只需尝试正确设置Content Type标题application/vnd.openxmlformats-officedocument.presentationml.presentation以防pptx,如下所示:

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.

答案 1 :(得分:1)

以下是Spring MVC Controller的一些示例代码:

@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。

通过这种方式,您可以完全不知道文件的创建版本是什么版本。

答案 2 :(得分:0)

我已在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();
        }
    }

从已保存的bytes文件中生成pptx

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文件。希望代码可以帮助您,如果您有任何疑问或疑问,那么我们可以讨论或者是否有任何建议。谢谢