将GBK编码的字符串转换为UTF-8编码
/**
* 将GBK编码转换成UTF-8编码
*
*/
protected String gbkConvertToUtf8(String str) throws UnsupportedEncodingException {
if (StringUtils.isNotBlank(str)) {
//转换成gbk编码
String gbkChinese = new String(str.getBytes("GBK"), StandardCharsets.ISO_8859_1);
String unicodeChinese = new String(gbkChinese.getBytes(StandardCharsets.ISO_8859_1), "GBK");
//中文
log.info(unicodeChinese);
//utf--8编码
String utf8Chinese = new String(unicodeChinese.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
//乱码
log.info(utf8Chinese);
unicodeChinese = new String(utf8Chinese.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//中文
log.info(unicodeChinese);
return unicodeChinese;
}else{
return null;
}
}