http://blog.csdn.net/shootyou/article/details/44852639

Emoji表情字符现在在APP已经广泛支持了。但是MySQL的UTF8编码对Emoji字符的支持却不是那么好。所以我们经常会遇到这样的异常:

  1. Incorrect string value: '\xF0\x90\x8D\x83...' for column

原因是Mysql里UTF8编码最多只能支持3个字节,而Emoji表情字符使用的UTF8编码,很多都是4个字节,有些甚至是6个字节。

解决的方案有两种:

1.使用utf8mb4的mysql编码来容纳这些字符。

2.过滤掉这些特殊的表情字符。

关于第一种解决方法,请参考:http://segmentfault.com/a/1190000000616820 和 http://info.michael-simons.eu/2013/01/21/Java-mysql-and-multi-byte-utf-8-support/

有大量细节需要注意,例如:mysql版本,mysql的配置,mysql connector的版本等等。。

因为我们使用的云数据库,所以我选择了过滤这些特殊字符。其实过滤的方式很简单,直接使用正则表达式匹配编码范围,然后替换就行了。

下面是我的代码。

更多可以参考:http://stackoverflow.com/questions/27820971/why-a-surrogate-java-regexp-finds-hypen-minus

  1. import org.apache.commons.lang3.StringUtils;
  2. public class EmojiFilterUtils {
  3. /**
  4. * 将emoji表情替换成*
  5. *
  6. * @param source
  7. * @return 过滤后的字符串
  8. */
  9. public static String filterEmoji(String source) {
  10. if(StringUtils.isNotBlank(source)){
  11. return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
  12. }else{
  13. return source;
  14. }
  15. }
  16. public static void main(String[] arg ){
  17. try{
  18. String text = "This is a smiley \uD83C\uDFA6 face\uD860\uDD5D \uD860\uDE07 \uD860\uDEE2 \uD863\uDCCA \uD863\uDCCD \uD863\uDCD2 \uD867\uDD98 ";
  19. System.out.println(text);
  20. System.out.println(text.length());
  21. System.out.println(text.replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "*"));
  22. System.out.println(filterEmoji(text));
  23. }catch (Exception ex){
  24. ex.printStackTrace();
  25. }
  26. }
  27. }

【转】【异常处理】Incorrect string value: '\xF0\x90\x8D\x83...' for column... Emoji表情字符过滤的Java实现的更多相关文章

  1. Incorrect string value: '\xF0\x90\x8D\x83...' for column 通用解决方案

    mysql插入非ascii字符时报这个错的根本原因在于: 对应表的字符集无法存储要插入的字符,比如汉字插入latin1编码,某些特殊字符插入gbk或者utf8等. 检查一下实际插入的字符以及对应表或者 ...

  2. Mysql之Incorrect string value: '\xF0\x9F\x98\x89 \xE6... 保存emoji表情

    错误信息如下: Incorrect string value: '\xF0\x9F\x98\x89 \xE6...' 问题产生的原因是字符串不兼容4字节的unicode导致的,一般我们常见的表情编码等 ...

  3. Incorrect string value: '\xF0\xA1\xA1\x92' for column 'herst' at row 1

    Incorrect string value: '\xF0\xA1\xA1\x92' for column 'herst' at row 1[转] 1.一般来说MySQL(小于5.5.3)字符集设置为 ...

  4. 让Mysql支持Emoji表情,解决[Err] 1366 - Incorrect string value: '\xF0\xA3\x84\x83'

    mysql insert内容包含表情或者unicode码时候,插入Mysql时失败了,报如下异常: java.sql.SQLException: Incorrect string value: '\x ...

  5. 表情存储异常--mybatis抛出异常(java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1)

    文章参考 https://blog.csdn.net/junsure2012/article/details/42171035 https://www.cnblogs.com/WangYunShuai ...

  6. java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'nick' at row 1

    java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'nick' at row 1 mysql报错 ...

  7. 解决pymysql.err.InternalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x8C\\xB8' for column 'headline' at row 1")

    解决pymysql.err.InternalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x8C\\xB8' for column ...

  8. java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xB3' for column 'Content' at row 1

    在尝试将 emoji 表情符号 插入MySQL数据库时,遇到以下错误信息: ### The error occurred while setting parameters ### SQL: INSER ...

  9. mysql 彻底解决:Incorrect string value: '\xF0\x9F\x98\xAD",...' for column 'commentContent' at row 1

    彻底解决:Incorrect string value: '\xF0\x9F\x98\xAD",...' for column 'commentContent' at row 1 今天在爬取 ...

随机推荐

  1. gcc-5.4.0 static dwarf2 compile

    ------------------------------------------------------------------------------- 又开始折腾了, 静态编译 gcc-5.4 ...

  2. 【sqlite】python备份数据库

    备份整个数据库的方法: # coding=utf-8 import sqlite3 def testBakSqlite(): conn = sqlite3.connect("sqlite_d ...

  3. HDU 5833 Zhu and 772002 (数论+高斯消元)

    题目链接 题意:给定n个数,这n个数的素因子值不超过2000,从中取任意个数使其乘积为完全平方数,问有多少种取法. 题解:开始用素筛枚举写了半天TLE了,后来队友说高斯消元才想起来,果断用模板.赛后又 ...

  4. xp系统打开软件程序总是弹出警告窗口,很烦人对不,怎么办呢?进来看

    为了不浪费比较着急的朋友的的时间,先把解决方案说了,下面我在细说: 细说: 今天装了个xp的虚拟机,为了不在xp里重复装真机(win7的)里已经有的软件,就把我的工具盘共享给了虚拟机,大部分软件都可以 ...

  5. selinux

    root@lujie ~]# vim /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # ...

  6. linux日常易忘指令

    1.编辑全局指令 进入~/.bash_profile 增加"exprot +(启动文件的地址)" source ~/.bash_profilr(刷新) 2.修改mysql密码 my ...

  7. Sql如何自动定时备份数据库

    直接上图

  8. asp.net 防止按钮重复提交

    1.将按钮属性设置如下: <asp:Button ID="btConfirm" runat="server" Text="Confirm&quo ...

  9. redis 列出所有的键

    > KEYS * (empty list or set)

  10. Cash Cow【dfs较难题应用】【sdut2721】

    Cash Cow Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdut ...