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

Java 通过http post下载文件将返回zip文件内容,java,spring-boot,jakarta-ee,Java,Spring Boot,Jakarta Ee,我可以看到很多相关的话题,但我有一个具体的问题。我正在使用SpringBootController下载一个zip文件。当文件是http verb get时,我可以下载它,但由于我必须传递一个大的json负载,所以我改为post。此后,它不再将其作为文件下载,而是使用一些ascii字符响应文件内容。下面是controller中下载文件的方法 @ApiResponses(value = { @ApiResponse(code = 404, message = "file could not be f

我可以看到很多相关的话题,但我有一个具体的问题。我正在使用SpringBootController下载一个zip文件。当文件是http verb 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);
}   
@ApiResponse(值={@ApiResponse(code=404,message=“无法找到文件”),
@ApiResponse(code=200,message=“文件已成功创建”)}
@PostMapping(path=“/download file/1.0”,products=“application/zip”)
公共响应下载文件(
@ApiParam(value=“文件创建内容”,required=true)@RequestBody InputDetailsVO InputDetailsVO){
File=null;
InputStreamResource资源=null;
HttpHeaders=新的HttpHeaders();
试一试{
//从zip文件创建InputStreamResource
资源=新的InputStreamResource(新文件InputStream(文件));
字符串contentType=“应用程序/zip”;
如果(!StringUtils.isEmpty(contentType)){
headers.setContentType(MediaType.parseMediaType(contentType));
}
headers.add(“内容处置”、“附件;文件名=\”+文件.getName()+“\”);
}捕获(例外e){
日志错误(“文件创建问题”,e);
}
返回ResponseEntity.ok()
.contentLength(文件.length())
.contentType(MediaType)
.parseMediaType(MediaType.APPLICATION\u八位字节\u流\u值))
.headers(headers).body(资源);
}   
下面是我得到的回应,而不是文件下载

    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
PK;��N
骚扰
30
test@test.com
主键�qB�@YYPK;��N�qB�@YYeEmployee details.xmlPKL�Y

像这样试试,你可以下载任何类型的文件。我假设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);
  }
@PostMapping(value=“/download file/1.0”,products=MediaType.APPLICATION\u OCTET\u STREAM\u value)
公共响应下载文件(@RequestBody InputDetailsVO InputDetailsVO){
String dirPath=“您的位置路径”;
byte[]fileBytes=null;
试一试{
字符串文件名=inputDetailsVO.getFileName();
fileBytes=Files.readAllBytes(path.get(dirPath+fileName));
}捕获(IOE异常){
e、 printStackTrace();
}
返回ResponseEntity.ok()
.contentType(MediaType.APPLICATION\u八位字节\u流)
.header(HttpHeaders.CONTENT\u处置,“附件;文件名=\”+文件名+“\”)
.body(文件字节);
}

像这样试试,你可以下载任何类型的文件。我假设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);
  }
@PostMapping(value=“/download file/1.0”,products=MediaType.APPLICATION\u OCTET\u STREAM\u value)
公共响应下载文件(@RequestBody InputDetailsVO InputDetailsVO){
String dirPath=“您的位置路径”;
byte[]fileBytes=null;
试一试{
字符串文件名=inputDetailsVO.getFileName();
fileBytes=Files.readAllBytes(path.get(dirPath+fileName));
}捕获(IOE异常){
e、 printStackTrace();
}
返回ResponseEntity.ok()
.contentType(MediaType.APPLICATION\u八位字节\u流)
.header(HttpHeaders.CONTENT\u处置,“附件;文件名=\”+文件名+“\”)
.body(文件字节);
}

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

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

        }
@RequestMapping(value=“/download”,method=RequestMethod.GET,products=“application/zip”)
公共响应下载(HttpServletResponse响应)引发IOException
{
//一些代码。。。
File File=新文件(“F:\\Folder\\Folder\\Folder\\\”+文件名);
InputStreamResource2=新的InputStreamResource(新文件InputStream(文件));
response.setContentType(“应用程序/zip”);
response.setHeader(“内容处置”,String.format(“内联;文件名=\”“+文件名+”\”);
response.setHeader(“responseType”、“arraybuffer”);
response.setHeader(“Content-Length”,“”+file.Length());
返回新的响应状态(resource2,HttpStatus.ACCEPTED);
}

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

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

        }
@RequestMapping(value=“/download”,method=RequestMethod.GET,products=“application/zip”)
公共响应下载(HttpServletResponse响应)引发IOException
{
//一些代码。。。
File File=新文件(“F:\\Folder\\Folder\\Folder\\\”+文件名);
InputStreamResource2=新的InputStreamResource(新文件InputStream(文件));
response.setContentType(“应用程序/zip”);
response.setHeader(“内容处置”,String.format(“内联;文件名=\”“+文件名+”\”);
response.setHeader(“responseType”、“arraybuffer”);
response.setHeader(“Content-Length”,“”+file.Length());
返回新的响应状态(resource2,HttpStatus.ACCEPTED);
}

我仍然看到相同的响应。你能发布InputDetailsVO类吗,我将生成json并检查。这是压缩文件的内容{“name”:“Harry”,“age”:“30”,“email”:test@test.com“,“active”:“y”}我只是将此数据转换为XML格式,并基于员工数量,我为每个员工生成一个xml文件并压缩内容以进行快速测试,只需硬编码压缩文件名,然后查看是否可以下载。我仍然看到相同的响应。您可以发布输入详细信息吗