【问题标题】:Spring REST file download unable to set header content-type attachmentSpring REST 文件下载无法设置标题内容类型附件
【发布时间】:2018-08-05 17:41:52
【问题描述】:

我有一个从给定目录下载文件的 Springboot REST 应用程序。 下载的文件可以是任意文件,任意格式,我想用原来的文件名作为下载文件的文件名。

我使用下面的代码在header中设置文件名,并将header添加到响应中:

@RestController
@RequestMapping("/downloads")
public class DownloadCsontroller {

...

        @GetMapping
        public void downloadSingleFile(@RequestParam("file") String filename, HttpServletResponse response) throws IOException {
            String filepath = m_attachmentPathLocation + File.separator + filename;
            File file = new File(filepath);
            String contentType = getContentType(file);
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType(contentType);
            response.setHeader("Content-Disposition:", "attachment;filename=\"" + file.getName() + "\"");
            ...
    }

...
}

在 setHeader() 中使用“Content-Disposition”和“Content-Disposition:”进行了测试。

除了 PDF、ZIP、RAR、EXE 等之外,几乎所有东西都可以正常工作(文件类型)。

任何不在列表中的文件(类型)都可以使用所需的文件名下载。 但是,当任何文件下载(PDF、ZIP、RAR、EXE 等)时……似乎它会像永远一样持续加载……我什至看不到 POSTMAN、inspector、firebug 等发送的任何请求。

如果我注释掉:

//response.setHeader("Content-Disposition:", "attachment;filename=\"" + file.getName() + "\"");

它可以工作,但文件名将设置为请求映射的名称。在这种情况下是“下载”。

我见过很多使用“Content-Disposition”标头来更改附件文件名的示例...但似乎在这些文件类型上失败了。

我还没有配置,这有点奇怪,因为在我搜索的大多数示例中......这应该正在运行或工作。

TIA

【问题讨论】:

    标签: spring-boot download spring-restcontroller spring-rest


    【解决方案1】:

    请添加@GetMapping(produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)

    而不是返回直接文件尝试返回流。

    另外请注意,如果请求的应用 IP 和端口号与服务器应用 IP 和端口号不同,则“Content-Disposition:”标头将不起作用。

    【讨论】:

      【解决方案2】:

      如果您可以通过使用 org.springframework.http.HttpHeaders 类设置所有标头值来稍微更改代码,一切都会奏效。 现在查看您的代码,我怀疑您试图公开一个允许下载多部分文件的 API。 我建议您不要使用 HttpServletResponse 类来设置 Content-Dispositionheader,而是使用 HttpHeaders 类。下面是重新格式化的代码

          @RestController
          public class DownloadCsontroller {
      
          @GetMapping(value="/downloads")
          public ResponseEntity<Object> downloadSingleFile(@RequestParam("file") 
          String filename) throws IOException {
          String filepath = m_attachmentPathLocation + File.separator + filename;
            File file = new File(filepath);
              String contentType = getContentType(file);
          /* response.setStatus(HttpServletResponse.SC_OK);
              response.setContentType(contentType);
              response.setHeader("Content-Disposition:", "attachment;filename=\"" 
           + file.getName() + "\"");
          */
          // Here is the below Code you need to reform for Content- 
          //Disposition and the remaining header values too.
          HttpHeaders headers= new HttpHeaders();
          headers.add("Content-Disposition", "attachment; filename 
          =whatever.pdf");
          headers.add("Content-Type",contentType);
      
         // you shall add the body too in the ResponseEntity Return object
         return new ResponseEntity<Object>(headers, HttpStatus.OK);
         }
         }
      

      【讨论】:

        猜你喜欢
        • 2011-01-15
        • 2019-08-22
        • 2014-03-15
        • 1970-01-01
        • 2020-08-06
        • 1970-01-01
        • 2019-07-20
        • 2023-03-10
        • 1970-01-01
        相关资源
        最近更新 更多