Knife4j series--solve the problem of garbled characters in downloaded files

Original website: Knife4j series--Solve the problem of garbled characters in downloaded files_IT Knife Unsheathed Blog-CSDN Blog

Introduction

illustrate

This article introduces how Knife4j solves the problem of not displaying the download button when downloading files, and returning garbled characters.

Related URLs

File download is always Knife4j.txt · Issue #I374SP · Xiao Ming/knife4j - Gitee.com

Notice

This problem can be solved in Knife4j2.x version. Since there is no description of the API interface produces in the OpenAPI3 specification, it cannot be solved. Therefore, it is recommended to use knife4j-spring-boot-starter dependency, which is version 2.x; it is not recommended to use knife4j-springdoc-ui dependency, which is version 3.x.

Problem recurrence

In SpringBoot, Excel is exported through the response flow, and the results are as follows (there is no download button, and the response content is directly garbled):

solution

Add produces = "application/octet-stream" to the @ApiOperation annotation.

as follows:

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

result

Click "Download File"

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/128346382