【问题标题】:Issue in downloading zip file in Spring REST Service在 Spring REST 服务中下载 zip 文件的问题
【发布时间】:2017-11-27 16:25:18
【问题描述】:

我的API如下:

@ApiOperation(value = "Zip of all the documents the customer attached to their application (id and loan)", notes = "", response = Void.class, tags = {
    "Manage Customers/Applications",
})
@ApiResponses(value = {
    @ApiResponse(code = 200, message = "OK", response = Void.class)
})
@RequestMapping(value = idPath + "/files/customer-documents/zip",
    method = RequestMethod.GET)
@ResponseBody
void downloadCustomerDocumentsAsZip(HttpServletResponse response,
                                    @ApiParam(value = "Application ID", required = true) @PathVariable(value = "applicationId")
                                        Long applicationId);

休息控制器:

 @Override
public void downloadCustomerDocumentsAsZip(HttpServletResponse response,
                                           @ApiParam(value = "Application ID", required = true) @PathVariable(value = "applicationId")
                                               Long applicationId) {

    InputStream inputStream = new ByteArrayInputStream(manageApplicationsService.findCustomerDocumentsAsZip(applicationId));

    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader("Content-Disposition", "attachment; filename=zipFile.zip");
    try {
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

}

回应:

PK{i�Jtemp0+I�(Q(A%

问题:

我想以附件的形式下载 zip 文件,但响应如上。

注意: 我尝试了Rest Download Endpoints 上解释的所有下载方法,但都没有成功。我也加了

产生 = MediaType.APPLICATION_OCTET_STREAM_VALUE

到 API 定义,但再次没有成功。

所以,如果有人能帮助我提供真正的解决方案,我将不胜感激。

【问题讨论】:

标签: spring rest spring-boot download zip


【解决方案1】:

我有同样的问题。将Content-Type 更改为MediaType.APPLICATION_PDF_VALUE 为我触发了下载操作。但是“另存为”对话框默认显示文件扩展名为.pdf

HttpServletResponse

        response.setContentType(MediaType.APPLICATION_PDF_VALUE);
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
        response.setContentLength((int)contents.length);
        try {
            response.getOutputStream().write(contents);
            response.getOutputStream().flush();
        } catch (IOException e) {
            throw new BadRequestException("Could not generate file");
        }

如果你使用ResponseEntity

    byte[] contents = fileContent.getBytes();
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
    responseHeaders.add(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_PDF_VALUE);
    return new ResponseEntity<byte[]>(contents, responseHeaders,HttpStatus.OK);

【讨论】:

  • 不客气。考虑现在清理...并删除不再需要的 cmets ;-)
【解决方案2】:

您可以在控制器中返回 ResponseEntity&lt;byte[]&gt;。将 Content-Type 和 Content-Disposition 标头添加到您的响应中,以便它正确打开。

    public ResponseEntity<byte[]> downloadCustomerDocumentsAsZip(
        @ApiParam(value = "Application ID", required = true)
        @PathVariable(value = "applicationId") Long applicationId) {

    byte[] bytes = manageApplicationsService.findCustomerDocumentsAsZip(applicationId);
    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("Content-Type", "application/octet-stream");
    headers.add("Content-Disposition", "attachment; filename=\"zipFile.zip\"");
    return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
   }

【讨论】:

  • 对于这个答案,我想知道'byte []'类型是否应该有任何Messageconverter?如果是这样,我需要哪一个?当我尝试应用代码时,但当我下载它们时,我的文件总是会损坏。 (我没有使用弹簧靴)。
【解决方案3】:

根据 HttpServletResponse 文档:调用 flush() 提交响应。如果您想使用 HttpServletResponse,我认为您需要致电 response.getOutputStream().flush();。否则,蒂姆的回答提供了一种更简单的方法。

【讨论】:

    【解决方案4】:

    您可以使用以下命令将媒体类型设置为:application/json;charset=UTF-8

    HttpHeaders header = new HttpHeaders();
    header.setContentType(MediaType.APPLICATION_JSON_UTF8);
    

    【讨论】:

      猜你喜欢
      • 2018-12-22
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2023-04-10
      • 2013-11-02
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多