java gbk转utf8

时间:2020-08-20 19:22:14   收藏:0   阅读:81

我的情况:

没有部署前的spring boot项目:前台请求的参数编码格式是utf8
在打成jar包后:前台请求的参数编码格式是gbk

 

代码:

String reqUrl = "";
String str = new String(reqUrl.getBytes(), "gbk");
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
reqUrl = new String(bytes);


其实可以先判断其编码格式,再进行编码格式的转换更好
String iso8859 = new String(reqUrl.getBytes("iso8859-1"));
String gbk = new String(reqUrl.getBytes("gbk"));
if(iso8859.equals(reqUrl)){
String str = new String(reqUrl.getBytes(), "iso8859-1");
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
reqUrl = new String(bytes);
System.out.println("iso8859");
}else if(gbk.equals(reqUrl)){
String str = new String(reqUrl.getBytes(), "gbk");
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
reqUrl = new String(bytes);
System.out.println("gbk");
}


欢迎纠错.........
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!