【转】【异常处理】Incorrect string value: '\xF0\x90\x8D\x83...' for column... Emoji表情字符过滤的Java实现
http://blog.csdn.net/shootyou/article/details/44852639
Emoji表情字符现在在APP已经广泛支持了。但是MySQL的UTF8编码对Emoji字符的支持却不是那么好。所以我们经常会遇到这样的异常:
- 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
- import org.apache.commons.lang3.StringUtils;
- public class EmojiFilterUtils {
- /**
- * 将emoji表情替换成*
- *
- * @param source
- * @return 过滤后的字符串
- */
- public static String filterEmoji(String source) {
- if(StringUtils.isNotBlank(source)){
- return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
- }else{
- return source;
- }
- }
- public static void main(String[] arg ){
- try{
- String text = "This is a smiley \uD83C\uDFA6 face\uD860\uDD5D \uD860\uDE07 \uD860\uDEE2 \uD863\uDCCA \uD863\uDCCD \uD863\uDCD2 \uD867\uDD98 ";
- System.out.println(text);
- System.out.println(text.length());
- System.out.println(text.replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "*"));
- System.out.println(filterEmoji(text));
- }catch (Exception ex){
- ex.printStackTrace();
- }
- }
- }

【转】【异常处理】Incorrect string value: '\xF0\x90\x8D\x83...' for column... Emoji表情字符过滤的Java实现的更多相关文章
- Incorrect string value: '\xF0\x90\x8D\x83...' for column 通用解决方案
mysql插入非ascii字符时报这个错的根本原因在于: 对应表的字符集无法存储要插入的字符,比如汉字插入latin1编码,某些特殊字符插入gbk或者utf8等. 检查一下实际插入的字符以及对应表或者 ...
- Mysql之Incorrect string value: '\xF0\x9F\x98\x89 \xE6... 保存emoji表情
错误信息如下: Incorrect string value: '\xF0\x9F\x98\x89 \xE6...' 问题产生的原因是字符串不兼容4字节的unicode导致的,一般我们常见的表情编码等 ...
- 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)字符集设置为 ...
- 让Mysql支持Emoji表情,解决[Err] 1366 - Incorrect string value: '\xF0\xA3\x84\x83'
mysql insert内容包含表情或者unicode码时候,插入Mysql时失败了,报如下异常: java.sql.SQLException: Incorrect string value: '\x ...
- 表情存储异常--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 ...
- 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报错 ...
- 解决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 ...
- 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 ...
- 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 今天在爬取 ...
随机推荐
- windows配置nginx实现负载均衡集群
windows配置nginx实现负载均衡集群2014-08-20 09:44:40 来源:www.abcde.cn 评论:0 点击:617 网上大部分关于nginx负载均衡集群的教程都是lin ...
- 【leetcode】 Interleaving String (hard)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- HDU 5833 Zhu and 772002 (数论+高斯消元)
题目链接 题意:给定n个数,这n个数的素因子值不超过2000,从中取任意个数使其乘积为完全平方数,问有多少种取法. 题解:开始用素筛枚举写了半天TLE了,后来队友说高斯消元才想起来,果断用模板.赛后又 ...
- mongochef如何链接有权限的mongodb3.x数据库
废话不多说,直接上图: 1.打开mongochef 2.打开的界面是这样的: 3.点击connect,上图红色框中的按钮,不要点下拉三角 4.点击New Connection按钮 5.1:上图标注1, ...
- python基础——使用模块
python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...
- python基础——模块
python基础——模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文 ...
- C --gettimeofday===获取某个程序经历的时间
#include <stdio.h> #include <sys/time.h> #include <time.h> int gettimeofday(struct ...
- Web 项目下载图片简单处理方式
1.如果图片头信息有 "Content-Disposition", "attachment; filename="fileName" 那么直接使用 i ...
- 八皇后(dfs+回溯)
重看了一下刘汝佳的白板书,上次写八皇后时并不是很懂,再写一次: 方法1:逐行放置皇后,然后递归: 代码: #include <bits/stdc++.h> #define MAXN 8 # ...
- iOS - 直播相关文章
直播相关文章 直播RTMP可用于测试的服务器地址 FFmpeg avdumpformat输出的tbn.tbc.tbr.PAR.DAR的含义 FFmpeg 3.0 计算视频时长 HLS Streamin ...