java - 通过 http post 下载文件将返回 zip 文件内容

标签 java spring-boot jakarta-ee

我可以看到许多相关主题,但我有一个特定的问题。我正在使用 Spring Boot Controller 下载 zip 文件。当文件是 http 动词 get 时,我可以下载该文件,但由于我必须传递一个大的 json 有效负载,所以我更改为 post。从那时起,它不再将其作为文件下载,而是使用一些 ASCII 字符响应文件的内容。下面是 Controller 中下载文件的方法。

@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

最佳答案

尝试这样,你可以下载任何类型的文件。我假设 InputDetailsVO 包含文件名,或者您可以有自己的逻辑来选择文件名。在这个方法之上,可以提供swagger相关的注解。

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

关于java - 通过 http post 下载文件将返回 zip 文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56479684/

相关文章:

java - 从 @RequestHeader 读取 JSON 对象

reactjs - React App + Spring Boot - Chrome 中未设置 cookie 内的 JWT 身份验证 token

spring-boot - Spring Boot 2.x 与 Camel 2.25 : Spring specific endpoints not working

java - 为什么我的实体管理器返回空结果列表?

java - Pom 文件中的这段代码是如何实现的呢?

java - 在 Java 的主要静态方法中调用非静态方法

java - 并行 IntStream 破解

java - 如何使用 spring DSL 在 Camel 中记录 header 值

java - 如何使用 Spring 将 session bean 注入(inject) POJO

java - 以有效的方式保存各种对象的 ArrayList