简体   繁体   English

通过http发布的文件下载将返回zip文件内容

[英]file download via http post is returning the zip file contents

I could see many related topics, but I have a specific problem. 我可以看到许多相关主题,但是我有一个特定的问题。 I am using spring boot controller to download a zip file. 我正在使用Spring Boot Controller下载一个zip文件。 I am able to download the file when it is http verb get, but as I have to pass a big json payload I changed to post. 当它是http verb get时,我能够下载该文件,但是由于我必须传递一个较大的json负载,因此我更改为发布。 Since then instead of downloading it as file it is responding the contents of the file with some ascii characters. 从那时起,而不是将其下载为文件,而是使用一些ascii字符来响应文件的内容。 Below is the method in controller for downloading the file. 以下是控制器中下载文件的方法。

@ApiResponses(value = { @ApiResponse(code = 404, message = "file could not be found"),
        @ApiResponse(code = 200, message = "File was created sucessfully") })
@PostMapping(path="/download-file/1.0", produces="application/zip")
public ResponseEntity<InputStreamResource> downloadFile(
        @ApiParam(value = "File creation contents", required = true) @RequestBody InputDetailsVO inputDetailsVO) {
    File file = null;
    InputStreamResource resource = null;
    HttpHeaders headers = new HttpHeaders();
    try {
        //Creating InputStreamResource out of zip file
        resource = new InputStreamResource(new FileInputStream(file));          
        String contentType = "application/zip";
        if (!StringUtils.isEmpty(contentType)) {
           headers.setContentType(MediaType.parseMediaType(contentType));
        }
        headers.add("Content-Disposition","attachment; filename=\""+file.getName()+"\"");
    } catch (Exception e) {
        log.error("Issue with file creation",e);

    }
    return ResponseEntity.ok()
            .contentLength(file.length())
            .contentType(MediaType
                          .parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE))
             .headers(headers).body(resource);
}   

Below is the response I am getting instead of file download 以下是我得到的答复,而不是文件下载

    PK;��N <?xml version="1.0" encoding="UTF-8"?>
<employeeDetails>
<name>Harry</name>
<age>30</30>
<email>test@test.com</test>
</employeeDetails>PK�qB�@Y;YPK;��N�qB�@Y;Yemployee details.xmlPKL�Y

Try like this, you can download any type of file. 尝试这样,您可以下载任何类型的文件。 I assume that InputDetailsVO contains the name of the file or you can have your own logic to pick the file name. 我假设InputDetailsVO包含文件名,或者您可以具有自己的逻辑来选择文件名。 On the top of this method, you can provide swagger related annotations. 在此方法的顶部,您可以提供与招摇相关的注释。

@PostMapping(value = "/download-file/1.0", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  public ResponseEntity<?> downloadFile(@RequestBody InputDetailsVO inputDetailsVO) {
    String dirPath = "your-location-path";
    byte[] fileBytes = null;
    try {
       String fileName = inputDetailsVO.getFileName();
      fileBytes = Files.readAllBytes(Paths.get(dirPath + fileName));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return ResponseEntity.ok()
        .contentType(MediaType.APPLICATION_OCTET_STREAM)
        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
        .body(fileBytes);
  }

I also had a similar use case. 我也有一个类似的用例。 I am sharing the code which had solved the issue. 我正在分享已解决问题的代码。

  @RequestMapping(value="/download",method=RequestMethod.GET,produces="application/zip" )
        public ResponseEntity<?> download(HttpServletResponse response) throws IOException
        {
            //Some Code...

            File file = new File("F:\\Folder\\Folder\\Folder\\"+filename); 
            InputStreamResource resource2 = new InputStreamResource(new FileInputStream(file));
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", String.format("inline; filename=\"" + filename + "\""));
            response.setHeader("responseType", "arraybuffer");
            response.setHeader("Content-Length", ""+file.length());

            return new ResponseEntity<InputStreamResource>(resource2,HttpStatus.ACCEPTED);

        }

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

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