通过http发布的文件下载将返回zip文件内容 [英] file download via http post is returning the zip file contents

查看:371
本文介绍了通过http发布的文件下载将返回zip文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I could see many related topics, but I have a specific problem. I am using spring boot controller to download a zip file. 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. Since then instead of downloading it as file it is responding the contents of the file with some ascii characters. 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

推荐答案

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

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

        }

这篇关于通过http发布的文件下载将返回zip文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆