简体   繁体   English

StreamingResponseBody 返回空文件

[英]StreamingResponseBody returning empty file

I'm trying to create a rest service to download files from a repository, using Springboot.我正在尝试使用 Springboot 创建一个休息服务来从存储库下载文件。

I'm trying to return a ResponseEntity with StreamingResponseBody, to return the file that i get from the repository, as an InputStream.我正在尝试返回一个带有 StreamingResponseBody 的 ResponseEntity,以返回我从存储库中获取的文件,作为 InputStream。

This is the current code i have:这是我当前的代码:


@GetMapping(path = "/downloadFile")
    public ResponseEntity<StreamingResponseBody> downloadFile(@RequestParam(value = "documentId") String documentId,
            HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {

        InputStream is = downloadService.getDocument(documentId);

        StreamingResponseBody out = outputStream -> {

            outputStream.write(IOUtils.toByteArray(is));
        };

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "text/csv");
        headers.add("Content-Disposition", "attachment; filename=" + documentId);
        headers.add("Pragma", "no-cache");
        headers.add("Cache-Control", "no-cache");

        return (new ResponseEntity<>(out, headers, HttpStatus.OK));

    }

When I consume this endpoint, using directly the browser, or postman, the file that is downloaded comes empty.当我直接使用浏览器或邮递员使用此端点时,下载的文件为空。 I understand that the OutputStream is written to asynchronously (Async is enabled in the config class).我知道 OutputStream 是异步写入的(在配置类中启用了 Async)。

How can I consume this service and get the file completely written, the way it comes from the repository I'm using ?我如何使用此服务并完全写入文件,它来自我正在使用的存储库的方式? ( if possible using Postman, just for testing purposes) (如果可能,使用 Postman,仅用于测试目的)

Am i building the service correctly?我是否正确构建了服务?

I have modified the code bit little, in my documentId is the name of the file to be downloaded.我稍微修改了代码,在我的 documentId 中是要下载的文件的名称。 I have tested, it is working fine.我已经测试过了,它工作正常。 Check below the code.检查下面的代码。

@GetMapping(path = "/downloadFile")
public ResponseEntity<StreamingResponseBody> downloadFile(
      @RequestParam(value = "documentId") String documentId,
      HttpServletRequest request,
      HttpServletResponse response)
      throws InterruptedException, IOException {
    String dirPath = "E:/sure-delete/"; //Directory having the files
    InputStream inputStream = new FileInputStream(new File(dirPath + documentId));
    final StreamingResponseBody out =
        outputStream -> {
          int nRead;
          byte[] data = new byte[1024];
          while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            System.out.println("Writing some bytes of file...");
            outputStream.write(data, 0, nRead);
          }
        };
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "text/csv");
    headers.add("Content-Disposition", "attachment; filename=" + documentId);
    headers.add("Pragma", "no-cache");
    headers.add("Cache-Control", "no-cache");
    return ResponseEntity.ok().headers(headers).body(out);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM