不知道什麼情況先編輯的全部沒有了
錯誤:不能向mysql插入4個和以上的字符,大多數是表情之類的比如:emoji表情
以前解決:是過濾emoji表情,但emoji表情ios android有些時候不同步,並且後面還有增加的可能,就有了以下解決方案
解決:
1.插入4個以下的
1.1查看unicode 和 utf-8編碼對應
// Unicode符號範圍 | UTF-8編碼方式
// (十六進制) | (二進制)
// --------------------+---------------------------------------------
// 0000 0000-0000 007F | 0xxxxxxx
// 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
// 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
// 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1.2過濾掉4個字節以上的字符
public static String fliterFourUnicode(String source) throws UnsupportedEncodingException {
byte[] sourceBytes;
sourceBytes = source.getBytes("utf-8");
int subIndex = 0;
String str = "";
do {
int curByte = Byte.toUnsignedInt(sourceBytes[subIndex]);
if (curByte > 0x00 && curByte <= 0x7f) { //0xxxxxxx
str = str + (char) sourceBytes[subIndex];
subIndex++;
} else if (curByte >= 0xc0 && curByte <= 0xdf) { //110xxxxx
byte[] bytes = { sourceBytes[subIndex], sourceBytes[subIndex + 1] };
str = str + new String(bytes, "utf-8");
subIndex += 2;
} else if (curByte >= 0xe0 && curByte <= 0xef) { //1110xxxx
byte[] bytes = { sourceBytes[subIndex], sourceBytes[subIndex + 1], sourceBytes[subIndex + 2] };
str = str + new String(bytes, "utf-8");
subIndex += 3;
} else if (curByte >= 0xf0 && curByte <= 0xf7) { //11110xxx
str = str + "*";
subIndex += 4;
} else if (curByte >= 0xf8 && curByte <= 0xfb) { //111110xx
str = str + "*";
subIndex += 5;
} else if (curByte >= 0xfc && curByte <= 0xfd) { //1111110x
str = str + "*";
subIndex += 6;
} else if (curByte >= 0xfe) { //11111110
str = str + "*";
subIndex += 7;
} else { //解析失敗不是UTF-8編碼開頭字符
return str + "*";
}
} while (subIndex < sourceBytes.length);
return str;
}
2.讓Mysql支持4個以上的,這種方案未測試過
2.1 修改字段類型爲 varchar
2.2 修改utf8_general_ci 到utf8mb4_general_ci
2.3 去掉jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&characterEncoding=UTF-8中的&characterEncoding=UTF-8
讓他自己適配,也可以升級高版本驅動