Knife4j系列--解决下载文件乱码的问题

原文网址:Knife4j系列--解决下载文件乱码的问题_IT利刃出鞘的博客-CSDN博客

简介

说明

本文介绍Knife4j如何解决下载文件不显示下载按钮,返回的是乱码的问题。

相关网址

文件下载始终是Knife4j.txt · Issue #I374SP · 萧明/knife4j - Gitee.com

注意

在Knife4j2.x版本中是可以解决这个问题的。由于OpenAPI3的规范中没有对API接口的produces描述,所以无法解决。所以建议使用knife4j-spring-boot-starter依赖,它是2.x版本的;不建议使用knife4j-springdoc-ui依赖,它是3.x版本的。

问题复现

在SpringBoot中,通过response流导出Excel,结果如下(没有下载按钮,响应内容中直接是乱码):

解决方案

在@ApiOperation注解中加上produces = "application/octet-stream"。

如下:

@ApiOperation(value = "导出库存数据", produces = "application/octet-stream")
@PostMapping("exportStorage")
public void exportStorage(@RequestBody StorageQueryBO storageQueryBO) {
    storageService.exportExcel(storageQueryBO);
}

结果

点击“下载文件”

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/128346382