Java中检测字符串的编码格式
1 public static String getEncoding(String str) { 2 String encode = "GB2312"; 3 try { 4 if (str.equals(new String(str.getBytes(encode), encode))) { 5 String s = encode; 6 return s; 7 } 8 } catch (Exception exception) { 9 } 10 encode = "ISO-8859-1"; 11 try { 12 if (str.equals(new String(str.getBytes(encode), encode))) { 13 String s1 = encode; 14 return s1; 15 } 16 } catch (Exception exception1) { 17 } 18 encode = "UTF-8"; 19 try { 20 if (str.equals(new String(str.getBytes(encode), encode))) { 21 String s2 = encode; 22 return s2; 23 } 24 } catch (Exception exception2) { 25 } 26 encode = "GBK"; 27 try { 28 if (str.equals(new String(str.getBytes(encode), encode))) { 29 String s3 = encode; 30 return s3; 31 } 32 } catch (Exception exception3) { 33 } 34 return ""; 35 }
越努力,越幸运!