【问题标题】:Spring OutputStream - download pptx with IESpring OutputStream - 用 IE 下载 pptx
【发布时间】:2017-02-11 05:49:00
【问题描述】:

我使用此 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)打开此文件

【问题讨论】:

    标签: java spring-mvc stream java-8


    【解决方案1】:

    只需尝试正确设置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.

    【讨论】:

      【解决方案2】:

      这是来自 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 版本的确切情况。

      【讨论】:

        【解决方案3】:

        我已经在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 文件。希望代码对您有所帮助,如果您有任何疑问或疑问,我们可以讨论或有任何建议。谢谢

        【讨论】:

          猜你喜欢
          • 2022-11-10
          • 1970-01-01
          • 2018-08-04
          • 2018-08-20
          • 1970-01-01
          • 2020-04-15
          • 1970-01-01
          • 1970-01-01
          • 2014-09-27
          相关资源
          最近更新 更多