1.判断中文字符串编码,返回编码
/**
* 判断中文字符串编码
*
* @param str
* @return
*/
public String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String code = encode;
return code;
}
} catch (Exception e) {
LOGGER.error("编码查询异常:{}", e);
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String code1 = encode;
return code1;
}
} catch (Exception e1) {
LOGGER.error("编码查询异常:{}", e1);
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String code2 = encode;
return code2;
}
} catch (Exception e2) {
LOGGER.error("编码查询异常:{}", e2);
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String code3 = encode;
return code3;
}
} catch (Exception e3) {
LOGGER.error("编码查询异常:{}", e3);
}
return "";
}
public static void main(String[] args)
{
System.out.println( getEncoding("鎴戞槸涓浗浜"));
System.out.println( getEncoding("D3D0D2BBCCECD4E7C9CFD0D1C0B4A3ACC8ABC7F2D2DFC7E9BDE1CAF8A3ACBDA1BFB5C2EBA3ACD0D0B3CCC2EBB9D8B1D5"));
}
2. 将编码结果打印到控制台
不管梦想有多远,总有一天,我们还回到那个最初的地方!