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

时间:2019-06-06 14:28:10

标签: java spring-boot java-ee

我可以看到许多相关主题,但是我有一个特定的问题。我正在使用Spring Boot Controller下载一个zip文件。当它是http verb get时,我能够下载该文件,但是由于我必须传递一个较大的json负载,因此我更改为发布。从那时起,而不是将其下载为文件,而是使用一些ascii字符来响应文件的内容。下面是控制器中下载文件的方法。

@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);
}   

以下是我收到的答复,而不是文件下载

    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

2 个答案:

答案 0 :(得分:0)

尝试这样,您可以下载任何类型的文件。我假设InputDetailsVO包含文件名,或者您可以具有自己的逻辑来选择文件名。在此方法的顶部,您可以提供与招摇相关的注释。

@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);
  }

答案 1 :(得分:0)

我也有一个类似的用例。我正在分享已解决问题的代码。

  @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);

        }