情况如下:
public static void main(String[] args) {
String orderDescription = "订购成功立即生效,30天内可观看专区中除单独计费影片外的所有内容,到期自动取消。";
orderDescription = Base64.encode(orderDescription, "gbk");
System.out.println(orderDescription);
orderDescription = orderDescription.replace("+", "%2B");
System.out.println(orderDescription);
orderDescription = Base64.decodeStr(orderDescription, "gbk");
System.out.println("==========================================");
System.out.println(orderDescription);
}
对这段中文进行编码后,出现了+号,导致,传给后端接口出现部分乱码现象
那么,导致这个问题的原因,就是Tomcat把+号换成了’ '空格。
这个是HTML规范中的规定,在这里算是个小坑,但是有不能说是bug
解决办法1:
对编码后的字符串进行替换
orderDescription = orderDescription.replace("+", "%2B");
那么,到后端接口后,会自动把 %2B 替换回+号,于是,可以正常解码了
来看下JDK中处理方式:
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
public static void main(String[] args) throws UnsupportedEncodingException {
String testStr="space here";
String afterEncode= URLEncoder.encode(testStr,"UTF-8");
System.out.println("1.beforeEncode: " + testStr);
System.out.println("2.afterEncode: "+afterEncode);
String afterDecode= URLDecoder.decode(afterEncode,"UTF-8");
System.out.println("3.afterDecode: "+afterDecode);
String replace = afterEncode.replace("+", "%20");
System.out.println("4.replace: "+replace);
String replaceDecode = URLDecoder.decode(replace, "UTF-8");
System.out.println("5.replaceDecode: "+replaceDecode);
}
参考:
这一篇讲的非常详细,提出了三种解决方案
https://www.cnblogs.com/thisiswhy/p/12119126.html