首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring OutputStream -使用IE下载pptx

Spring OutputStream -使用IE下载pptx
EN

Stack Overflow用户
提问于 2016-10-03 17:59:00
回答 3查看 1K关注 0票数 6

我使用以下Java代码从web应用程序下载文件:

代码语言:javascript
运行
复制
 @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)打开此文件

EN

回答 3

Stack Overflow用户

发布于 2016-10-08 20:06:10

只需尝试正确设置Content Type标头,如果是pptx,则设置为application/vnd.openxmlformats-officedocument.presentationml.presentation,如下所示:

代码语言:javascript
运行
复制
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.

票数 3
EN

Stack Overflow用户

发布于 2016-10-12 05:45:39

下面是来自Spring MVC控制器的一小段示例代码:

代码语言:javascript
运行
复制
@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创建的。

票数 1
EN

Stack Overflow用户

发布于 2016-10-12 17:37:21

我已经在IE-11中测试了代码,它工作正常。参见以下代码,即

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
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文件了。希望代码能对你有所帮助,如果你有任何疑问或疑问,我们可以讨论或提出任何建议。谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39828967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档