【问题标题】:downloading file from Spring Rest Controller从 Spring Rest Controller 下载文件
【发布时间】:2017-05-17 14:46:15
【问题描述】:

我正在尝试通过 Spring REST 控制器下载文件。下面是我的代码 -

@RequestMapping(value="/aaa",method=RequestMethod.GET,produces=MediaType.APPLICATION_OCTATE_STREAM_VALUE) 
public ResponseEntity<byte[]> testMethod(@RequestParam("test") String test) {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentDispositionFromData("attachment","testExcel.xlsx");
    responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    File file = new File("C:\\testExcel.xlsx");
    Path path = Paths.get(file.getAbsolutePath());
    ByteArrayResource resource = new ByteArrayResource(Files.readAllbytes(path));
    return new ResposeEntity<byte[]>(resource.getByteArray(),responseHeaders,HttpStatus.OK);
}

这在按钮点击中被调用。单击按钮后,没有任何反应。在调试这个 java 时,我可以看到字节流。在 Mozilla 的开发人员工具中,我可以看到成功的 HTTP 响应(该 excel 文件的字节流中的响应)。根据互联网上可用的资源,浏览器应该会自动下载文件,但这并没有发生。

为什么没有在浏览器中进行下载?我还需要做什么才能让它发挥作用?

注意:这仅用于 POC 目的。实际上,我必须通过 Apache POI 或其他一些 API 从数据库详细信息中生成 excel 文件。

【问题讨论】:

    标签: excel spring rest controller download


    【解决方案1】:

    以下对我有用:

    @RequestMapping("/")
    public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
    
        FileInputStream inputStream = new FileInputStream(new File("C:\\work\\boot\\pom.xml"));
    
        response.setHeader("Content-Disposition", "attachment; filename=\"testExcel.xlsx\"");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(inputStream, outputStream);
    
        outputStream.close();
        inputStream.close();
    }
    

    【讨论】:

    • 不使用HttpServletRequest和HttpServletResponse,不是可以吗?我在这里使用 ResponseEntity。
    • 您只需要写入响应输出流,这是关键。您不需要此示例中的请求。不需要的响应实体。此代码有效:)
    • 我仍然在响应中收到字节流。文件不会自动下载到浏览器中。我从 Angular js 调用这个控制器。 angularjs有什么需要做的吗?
    【解决方案2】:

    在我的情况下起作用的是让服务器发送以下标头:

    Access-Control-Expose-Headers: Content-Disposition
    

    所以我的问题与 CSRF 问题有关。

    【讨论】:

      【解决方案3】:

      像这样更改代码:

      public HttpEntity<byte[]> testMethod(@RequestParam("test") String test) {
      
          File file = new File("C:\\testExcel.xlsx");
          FileInputStream fileInputStream = new FileInputStream(file);
          byte[] array = IOUtils.toByteArray(fileInputStream);
      
          HttpHeaders header = new HttpHeaders();
          header.set("Content-type","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
          header.set("Content-Disposition", "attachment; filename=" + nameyouwantsaveyourfile + ".xlsx");
          header.setContentLength(array.length);
          fileInputStream.close();
          return new HttpEntity<byte[]>(array, header);
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-11
        • 2019-01-02
        • 1970-01-01
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多