java gbk转utf8
2021-04-01 21:24
标签:gbk utf8 print char nbsp 请求 没有 编码格式 standard 我的情况: 没有部署前的spring boot项目:前台请求的参数编码格式是utf8 代码: java gbk转utf8 标签:gbk utf8 print char nbsp 请求 没有 编码格式 standard 原文地址:https://www.cnblogs.com/hello-dummy/p/13525758.html
在打成jar包后:前台请求的参数编码格式是gbkString 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");
}
欢迎纠错.........