首页 > 其他分享 >swagger下载文件名中文乱码、swagger导出文件名乱码、swagger文件导出名称乱码

swagger下载文件名中文乱码、swagger导出文件名乱码、swagger文件导出名称乱码

时间:2024-06-19 16:28:52浏览次数:12  
标签:core swagger plugin spring 文件名 springframework 乱码 springfox

文章目录

一、场景描述:swagger导出文件名称乱码

场景描述:springboot项目集成swagger2.9.2后,下载文件时若文件名有中文则乱码。

(1)依赖如下

<!--swagger2-->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.9.2</version>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.9.2</version>
</dependency>

(2)下载文件乱码如下
在这里插入图片描述
(3)java代码如下

@Controller
@RequestMapping("/file")
public class FileController {

    @GetMapping("/export1")
    public void printSubmit1( HttpServletResponse response) throws Exception {
        //获取模板位置
        InputStream templateFileName = getClass().getResourceAsStream("/template/dataSourceDetailExport.xlsx");
        String fileName = "测试文件";
        List<List<String>> dataList = new ArrayList<>();
        for(int i=0;i<3;i++){
            List<String> data = new ArrayList<>();
            for(int j=1;j<10;j++){
                data.add(i+""+j);
            }
            dataList.add(data);
        }
        try {
            //对文件名进行编码,防止中文乱码
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
            OutputStream os = response.getOutputStream();
            EasyExcel.write(os).withTemplate(templateFileName).sheet().doWrite(dataList);;
        } catch (IOException e) {
            throw new Exception("导出excel表格失败!", e);
        }
    }
}

二、乱码原因

这是由于sweagger2.9.2版本问题导致的,在swagger2.9.2中下载是乱码的,但是直接在浏览器中输入请求下载就是正常的。

三、解决方法

3.1、方法一、在浏览器中输入地址下载

在这里插入图片描述

3.2、方法二、swagger升级为2.10.0及以上

需要将swagger升级为2.10.0及以上
重点:需要swagger包含的spring-plugin-core包是2.0.0.RELEASE版本,swagger包含的spring-plugin-metadata包是2.0.0.RELEASE版本

(1)依赖如下

<!--swagger2-->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-spring-webmvc</artifactId>
	<version>2.10.0</version>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.10.0</version>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.10.0</version>
</dependency>

(2)swagger配置如下

package com.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig
{


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.demo")) //你自己的package
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("小工具"+"\t"+new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
                .description("docker-compose")
                .version("1.0")
                .termsOfServiceUrl("")
                .build();
    }
}

(3)下载结果如下图
在这里插入图片描述

四、可能遇到的问题

4.1、DocumentationPluginsManager.java:152

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

springfox.documentation.spring.web.plugins.DocumentationPluginsManager.createContextBuilder(DocumentationPluginsManager.java:152)

The following method did not exist:

org.springframework.plugin.core.PluginRegistry.getPluginFor(Ljava/lang/Object;Lorg/springframework/plugin/core/Plugin;)Lorg/springframework/plugin/core/Plugin;

The method's class, org.springframework.plugin.core.PluginRegistry, is available from the following locations:

jar:file:/D:/maven/MavenRepository/org/springframework/plugin/spring-plugin-core/2.0.0.RELEASE/spring-plugin-core-2.0.0.RELEASE.jar!/org/springframework/plugin/core/PluginRegistry.class

The class hierarchy was loaded from the following locations:

org.springframework.plugin.core.PluginRegistry: file:/D:/maven/MavenRepository/org/springframework/plugin/spring-plugin-core/2.0.0.RELEASE/spring-plugin-core-2.0.0.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.plugin.core.PluginRegistry

若遇到以上问题,则先在swagger中排除以下依赖,并手动引入以下版本的依赖

<dependency>
	<artifactId>spring-plugin-core</artifactId>
	<groupId>org.springframework.plugin</groupId>
	<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
	<artifactId>spring-plugin-metadata</artifactId>
	<groupId>org.springframework.plugin</groupId>
	<version>2.0.0.RELEASE</version>
</dependency>

注意: spring-plugin-core-2.0.0.RELEASE版本需要与swagger2.10.0及以上版本配合使用。若swagger版本为2.9.2及以下,需要用低版本的spring-plugin-core-1.2.0.RELEASE版本

标签:core,swagger,plugin,spring,文件名,springframework,乱码,springfox
From: https://blog.csdn.net/weixin_49114503/article/details/139805076

相关文章

  • 解决 执行 jar 命令 控制台乱码
    Springboot项目,编码为utf8打包后,为了在控制台运行时不乱码,需要在控制台中依次执行以下命令:第一步:chcp65001第二步:java-jar-Dfile.encoding=utf-8你的.jar......
  • win——cmd窗口下执行jar包 logger.info输出乱码
    1、问题描述执行jar包,在cmd窗口中执行jar包,看到输出中,logger.info的输出为乱码,而System.out.println()输出为正确的日志。2、问题处理2.1、窗口设置因为cmd默认的编码为GBK,jar包一般设置为UTF-8;在cmd窗口先输入:chcp65001(注意之间有空格)然后执行jar包命令:java-jar-Dfil......
  • DolphinScheduler日志乱码、worker日志太多磁盘报警、版本更新导致不兼容怎么办?
    作者|刘宇星本文作者总结了在使用ApacheDolphinScheduler过程中遇见过的常见问题及其解决方案,包括日志出现乱码、worker日志太多磁盘报警、版本更新导致不兼容问题等,快来看看有没有困扰你想要的答案吧!DolphinScheduler集群环境有多台worker(worker1,worker2,worker3),多个......
  • 解决 JMeter 返回内容中文乱码问题的详细指南
    前言在使用ApacheJMeter进行性能测试时,处理中文字符可能会遇到乱码问题。这不仅影响测试结果的正确性,还会导致测试报告难以理解。本文将详细介绍如何解决JMeter返回内容中的中文乱码问题,从配置文件设置到编码转换,帮助测试工程师顺利进行性能测试。常见的中文乱码问题在JM......
  • 如何解决c++使用mysql数据库读取中文输出时乱码问题
    使用vs写c++输出数据库中文时出现乱码设置utf-8还是不行这是数据库的内容这是输出:在网上找有说改成utf-8的格式,加入这样一句:mysql_query(conn,"setnamesutf8");效果就是这样:发现错误依旧,询问群友后修改,需要在连接数据库的函数中加入如下一句:mysql_query(conn,"set......
  • 删除多个文件名中相同的指定字段
    删除多个文件名中相同的指定字段删除"HC1201150"字段:importosdefdelete_files_with_string(directory,substring):forroot,_,filesinos.walk(directory):forfileinfiles:ifsubstringinfile:full_path=os.path.......
  • 【工具推荐】MobaXterm远程终端管理工具史上最全攻略,涉及下载、安装、字体等配置、解
    【强烈推荐】MobaXterm远程终端管理工具史上最全攻略,涉及下载、安装、字体等配置、解决中文乱码、Telnet/ssh/Serial使用教程、高级功能使用技巧等。MobaXterm是一个增强型的Windows终端。其为Windows桌面提供所有重要的远程网络终端工具(如SSH、X11、RDP、VNC、FTP、S......
  • ffmpeg封装和解封装介绍-(9)根据输入时间参数和文件名调整截断时间
    头文件:xformat.h#pragmaonce///<summary>///封装和解封装基类///</summary>#include<mutex>structAVFormatContext;structAVCodecParameters;structAVPacket;structXRational{intnum;///<Numeratorintden;///<Denominator......
  • [BUUCTF_Misc]乌镇峰会种图,但十六进制对应文本乱码
    题目地址:BUUCTF乌镇峰会种图使用工具:edge浏览器、HxDHexEditor主要问题:edgeimageviewer保存图片后用十六进制编辑器打开,对应文本中含有大量乱码问题状态:题已做完,但不懂为什么点击附件后默认在edge浏览器的edgeimageviewer中查看图片,鼠标右键另存为图像。单看图片感觉......
  • 【Java】 探索Java中遍历文件夹的奥秘:获取文件夹内所有文件名
    >>【痕迹】QQ+微信朋友圈和聊天记录分析工具>>(1)纯Python语言实现,使用Flask后端,本地分析,不上传个人数据。>>(2)内含QQ、微信聊天记录保存到本地的方法,真正实现自己数据自己管理。>>(3)数据可视化分析QQ、微信聊天记录,提取某一天的聊天记录与大模型对话。>>下载地......