一、说明
1、springboot版本2.2.13.RELEASE
2、本来是没有乱码,不知道什么时候增加了其他的依赖,即dependency,就导致乱码了
3、配置server.tomcat.uri-encoding=UTF-8,无效
4、配置spring.http.encoding.charset=UTF-8,无效
5、解决:自定义filter处理表单参数
6、我的情况是真神奇,有的接口是utf8,有的接口是8859;恶心坏了;改造了下代码,临时解决下吧
二、copy复制
package com.ruoyi.hg.filter;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.text.CharsetKit;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Order(Integer.MIN_VALUE)
@Component
public class EncodingFilter extends OncePerRequestFilter {
private String encoding;
private boolean forceEncoding = false;
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void setForceEncoding(boolean forceEncoding) {
this.forceEncoding = forceEncoding;
}
public String filter(String input) {
return CharsetKit.convert(input, CharsetKit.ISO_8859_1, CharsetKit.UTF_8);
}
@Override
protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
// 设置request和response的编码格式(解决post请求乱码)
// 对request中的参数进行编码格式的转换(解决get请求乱码)
filterChain.doFilter(new HttpServletRequestWrapper(request) {
@Override
public String getParameter(String name) {
String value = super.getParameter(name);
return check8859(value);
}
@Override
public String[] getParameterValues(String name) {
String[] values = super.getParameterValues(name);
if (values == null) {
return null;
}
String value = null;
for (int i = 0; i < values.length; i++) {
value = values[i];
// 不包含,说明转成功了
values[i] = check8859(value);
}
return values;
}
}, response);
}
String check8859(String val) {
if (ObjectUtil.isNull(val)) {
return val;
}
String afterValue = filter(val);
if (afterValue.contains("?")) {
// utf8文字,强行通过 ISO_8859_1 -> utf8,会包含问好
return val;
}
// 不包含,说明转成功了
return afterValue;
}
}
三、补一个类CharsetKit
若依的框架, 再缺其他,可以到ruoyi.vip下载源码找一找
package com.ruoyi.common.utils.text;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.ruoyi.common.utils.StringUtils;
/**
* 字符集工具类
*
* @author ruoyi
*
*/
public class CharsetKit
{
/** ISO-8859-1 */
public static final String ISO_8859_1 = "ISO-8859-1";
/** UTF-8 */
public static final String UTF_8 = "UTF-8";
/** GBK */
public static final String GBK = "GBK";
/** ISO-8859-1 */
public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO_8859_1);
/** UTF-8 */
public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8);
/** GBK */
public static final Charset CHARSET_GBK = Charset.forName(GBK);
/**
* 转换为Charset对象
*
* @param charset 字符集,为空则返回默认字符集
* @return Charset
*/
public static Charset charset(String charset)
{
return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
}
/**
* 转换字符串的字符集编码
*
* @param source 字符串
* @param srcCharset 源字符集,默认ISO-8859-1
* @param destCharset 目标字符集,默认UTF-8
* @return 转换后的字符集
*/
public static String convert(String source, String srcCharset, String destCharset)
{
return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));
}
/**
* 转换字符串的字符集编码
*
* @param source 字符串
* @param srcCharset 源字符集,默认ISO-8859-1
* @param destCharset 目标字符集,默认UTF-8
* @return 转换后的字符集
*/
public static String convert(String source, Charset srcCharset, Charset destCharset)
{
if (null == srcCharset)
{
srcCharset = StandardCharsets.ISO_8859_1;
}
if (null == destCharset)
{
destCharset = StandardCharsets.UTF_8;
}
if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset))
{
return source;
}
return new String(source.getBytes(srcCharset), destCharset);
}
/**
* @return 系统字符集编码
*/
public static String systemCharset()
{
return Charset.defaultCharset().name();
}
}