字符编码容错
服务器不同的配置造成乱码,一种简单的编码容错方案
* 判断字符是否是中文
*
* @param c 字符
* @return 是否是中文
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* 判断字符串是否是乱码
*
* @param strName 字符串
* @return 是否是乱码
*/
public static boolean isMessyCode(String strName) {
Pattern p = Pattern.compile("\s*|t*|r*|n*");
Matcher m = p.matcher(strName);
String after = m.replaceAll("");
String temp = after.replaceAll("\p{P}", "");
char[] ch = temp.trim().toCharArray();
float chLength = ch.length;
float count = 0;
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (!Character.isLetterOrDigit(c)) {
if (!isChinese(c)) {
count = count + 1;
}
}
}
float result = count / chLength;
if (result > 0.4) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
System.out.println(isMessyCode("韩顺平"));
System.out.println(isMessyCode("你好"));
}
====================================================
//字符是否由中文字母和数字组成
boolean isRightNickName = true;
String regex = "^[a-z0-9A-Z\u4e00-\u9fa5]+$";
isRightNickName = nickname.matches(regex);
//乱码转utf8
if(isRightNickName==true){
String nickname_utf8 = new String(nickname.getBytes("iso-8859-1"), "utf-8");
isRightNickName = nickname_utf8.matches(regex);
if(isRightNickName==true){
nickname_utf8 = new String(nickname.getBytes("gbk"), "utf-8");
isRightNickName = nickname_utf8.matches(regex);
}
}