正常操作评论,保存时,若评论内容含有特殊表情符号,后台将报错如下:
Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='; nested exception is java.sql.SQLException: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='
后来查阅资料,找到方法将其评论内容中特殊符号过滤掉,然后保存。废话不多说,直接上代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 检测是否有emoji字符 3 * @param source 4 * @return 一旦含有就抛出 5 */ 6 public static boolean containsEmoji(String source) { 7 if (StringUtils.isBlank(source)) { 8 return false; 9 } 10 11 int len = source.length(); 12 13 for (int i = 0; i < len; i++) { 14 char codePoint = source.charAt(i); 15 16 if (isEmojiCharacter(codePoint)) { 17 //do nothing,判断到了这里表明,确认有表情字符 18 return true; 19 } 20 } 21 22 return false; 23 } 24 25 private static boolean isEmojiCharacter(char codePoint) { 26 return (codePoint == 0x0) || 27 (codePoint == 0x9) || 28 (codePoint == 0xA) || 29 (codePoint == 0xD) || 30 ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || 31 ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || 32 ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)); 33 } 34 35 /** 36 * 过滤emoji 或者 其他非文字类型的字符 37 * @param source 38 * @return 39 */ 40 public static String filterEmoji(String source) { 41 42 if (!containsEmoji(source)) { 43 return source;//如果不包含,直接返回 44 } 45 //到这里铁定包含 46 StringBuilder buf = null; 47 48 int len = source.length(); 49 50 for (int i = 0; i < len; i++) { 51 char codePoint = source.charAt(i); 52 53 if (isEmojiCharacter(codePoint)) { 54 if (buf == null) { 55 buf = new StringBuilder(source.length()); 56 } 57 58 buf.append(codePoint); 59 } else { 60 } 61 } 62 63 if (buf == null) { 64 return source;//如果没有找到 emoji表情,则返回源字符串 65 } else { 66 if (buf.length() == len) {//这里的意义在于尽可能少的toString,因为会重新生成字符串 67 buf = null; 68 return source; 69 } else { 70 return buf.toString(); 71 } 72 } 73 74 }