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. Win7下同时使用有线和无线时的优先级设置

    终于找到这个问题的解决方案了!!!!我是通过方法1改跃点数实现的,方法2无效. http://linshengling.blog.163.com/blog/static/114651912012102 ...

  2. 网页中超链接的简化问题(如何避免超链接的get提交)

    废话不多,直接看例子吧: <a href="###?key=${*** }">跳转</a> 上面这个超链接被点击后,肯定会向某个控制器发送一个get请求,而 ...

  3. QQ右下角图标不见了

    [QQ]我的qq是在线的,但是桌面右下角的企鹅小图标却不见了??? 最好的办法是:CTRL+ALT+Z,先把QQ的控制面板调出来 然后点菜单,选设置,系统设置.在基本设置的选项卡中,窗口设置的最后一项 ...

  4. 新浪微博授权失败:applications over the unaudited use restrictions

    在用新浪微博授权第三方app时,授权失败,log显示 com.sina.weibo.sdk.exception.WeiboHttpException: {,"request":&q ...

  5. ASM:《X86汇编语言-从实模式到保护模式》1-4章:处理器,内存和硬盘基础

    其实很久之前就学完了实模式了,但是一直没有总结,感觉现在直接在书上做笔记的弊端就是有些知识点不能很很深刻地记下来(毕竟手写最明显的优点就是能深刻地记住知识,但是就是用太多的时间罢了).一下内容都是一些 ...

  6. 【python】类中的super().__init__()

    来源:百度知道 python中的super( test, self).__init__()是什么意思? 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后&qu ...

  7. [Android Pro] Test win

    http://www.cnblogs.com/mayingbao/ http://www.cnblogs.com/hyddd/

  8. Vector_h

    #ifndef VECTOR_H #define VECTOR_H #include <algorithm> template<typename Object> class V ...

  9. MAC下GitHub命令操作

    由于GitHub实在太有用了~~ ,各种源代码,开源工程,经常需要下载下来使用和学习,或者自己的代码需要上传之类的,尽管有"GitHub for Mac"工具,但是作为一名程序猿! ...

  10. SOCKet 编程 简介

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...