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. Lake Counting_深度搜索_递归

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30414   Accepted: 15195 D ...

  2. FindinFiles - Windows文件内查找插件

    FindInFiles for Windows 今天分享一个不错的插件工具:FindInFiles.如其名,其功能和Visual Studio的Ctrl+H快捷键类似,方便Windows使用者在资源管 ...

  3. Mac会给你一些欣喜

    Mac会给你一些欣喜 以前一直没有用过Mac,一直都是用Windows的电脑,只是偶尔会去用Ubuntu这样的Linux系统.Mac OS 确实是一只可以给你欣喜的系统. 上周拿到公司分发的Mac,到 ...

  4. div+css进度条

    效果图: 进度条代码: <style type="text/css"> 红色:background-color:f05153:border:1px solid #f05 ...

  5. bootstrap-datepicker的使用

    转载自:http://michael-roshen.iteye.com/blog/1779541 在普通的网页中显示datepicker比较简单,将bootstrap-datepicker-zh_CN ...

  6. IOS - 真机测试

    转:版权声明:本文由本人亲自一步步完成,并细心排版生成,望广大同仁尊重一下本人的劳动成果,转载请注明出处,原文地址http://my.oschina.net/joanfen/blog/167730 一 ...

  7. AutoLayout 图解各种约束

  8. supersr--时间显示逻辑-->NSDate+NSCalendar

    一种:时间逻辑: - (NSString *)created_at{ //    从后台返回的字符串格式:Mon Aug 03 09:17:31 +0800 2014, //NSDateFormatt ...

  9. 通过颜色代码初始化UIColor

    #define UIColorFromHEX(rgbValue) [UIColor \ colorWithRed:((float)((rgbValue & 0xFF0000) >> ...

  10. AppStore下载失败使用已购页面再试一次解决方法

    AppStore载失败 使用已购页面再试一次解决方法 工具/原料 Mac OS 方法/步骤 1.大家可以先试试更改系统 DNS 的方法,由于苹果的 App Store 应用商店在国外,所以 DNS 如 ...