•  

emoji表情特殊字符出错处理

2017-12-02

本人接入了QQ第三方登录,大家知道,QQ名字五花八门的,里面会包含表情符,其他语种等各种特别字符,而存储到我本地数据库就包错了,错误信息:

java.lang.RuntimeException: com.jfinal.plugin.activerecord.ActiveRecordException: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8D\x8A\xE3\x80...' for column 'uname' at row 1
com.jfinal.plugin.activerecord.ActiveRecordException: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8D\x8A\xE3\x80...' for column 'uname' at row 1
at com.jfinal.plugin.activerecord.Model.save(Model.java:442)
at com.sl.sys.user.mode.SysUser.doRegist(SysUser.java:80)
at com.sl.sys.sso.service.OpenIdService.saveOpenId(OpenIdService.java:71)
at com.sl.sys.sso.service.OpenIdService.userByQQ(OpenIdService.java:42)
at sun.reflect.GeneratedMethodAccessor296.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sl.basic.dubbo.service.imp.SLDubboServiceImp.executeSerive(SLDubboServiceImp.java:44)
at com.sl.basic.dubbo.service.imp.SLDubboServiceImp$$FastClassByCGLIB$$9f36a09.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at com.jfinal.aop.Invocation.invoke(Invocation.java:80)
at com.jfinal.plugin.activerecord.tx.Tx.intercept(Tx.java:77)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at com.jfinal.aop.Callback.intercept(Callback.java:85)
at com.sl.basic.dubbo.service.imp.SLDubboServiceImp$$EnhancerByCGLIB$$476cdf12.executeSerive(<generated>)
at com.alibaba.dubbo.common.bytecode.Wrapper0.invokeMethod(Wrapper0.java)
at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:46)
at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:72)
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)
at com.sl.basic.dubbo.auth.SLAuthorityProviderFilter.invoke(SLAuthorityProviderFilter.java:83)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:64)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:60)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:112)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:108)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:84)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:170)
at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:52)
at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8D\x8A\xE3\x80...' for column 'uname' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2444)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2347)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at com.jfinal.plugin.activerecord.Model.save(Model.java:437)
... 44 more


查了网上说是有两种做法

第一,修改数据库字符集: 
这种方法需要的硬性要求就是你的mysql数据库版本5.5以后的。一般有数据库管理工具的,直接打开改了就是了,比如我用的HeidiSQL,直接把表改为utf8mb4就可以了。这种方法简单省事,但是可能需要重启数据库。还有个问题是,有时候这方法不太灵。


第二,将这些表情过滤掉 
既然数据库不能保存,那就直接把这些表情过滤掉好了。这种情况是损坏客户的个性而让服务更便捷的一张方式。目前很多网站就是这么干的,毕竟效率是关键,你这表情即便保存了,也说不定哪里再次用到,展示不了。所以还是过滤emoji字符吧。

来一个简化版的

package com.sl.basic.util;

import org.apache.commons.lang3.StringUtils;

/**
 *emoji工具过滤类
 */
public class SLEmojiFilter {
    /**
     * 检测是否有emoji字符
     *
     * @param source
     * @return 一旦含有就抛出
     */
    public static boolean containsEmoji(String source) {
        if (StringUtils.isBlank(source)) {
            return false;
        }
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isEmojiCharacter(codePoint)) {
                //do nothing,判断到了这里表明,确认有表情字符
                return true;
            }
        }
        return false;
    }

    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) ||
                (codePoint == 0x9) ||
                (codePoint == 0xA) ||
                (codePoint == 0xD) ||
                ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
                ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    /**
     * 过滤emoji 或者 其他非文字类型的字符
     *
     * @param source
     * @return
     */
    public static String filterEmoji(String source) {
        source = source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");
        if (!containsEmoji(source)) {
            return source;//如果不包含,直接返回
        }
        //到这里铁定包含
        StringBuilder buf = null;

        int len = source.length();

        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);

            if (isEmojiCharacter(codePoint)) {
                if (buf == null) {
                    buf = new StringBuilder(source.length());
                }

                buf.append(codePoint);
            } else {
                buf.append("*");
            }
        }

        if (buf == null) {
            return source;//如果没有找到 emoji表情,则返回源字符串
        } else {
            if (buf.length() == len) {//这里的意义在于尽可能少的toString,因为会重新生成字符串
                buf = null;
                return source;
            } else {
                return buf.toString();
            }
        }

    }
    
}


因为这里不能输入emoji字符,因此我只能截图了

测试代码

结果

如果想下载完整里面包含emoji字符,可以进行测试),可以点击下载 SLEmojiFilter.java


再来一个更强大的

github开源了一个框架(emoji-java),提供的方法更多,功能更强大,如下图。

有时候github无法打开,因此你可以从这里下载emoji 和依赖的json包

申明:本文受法律保护,未经作者同意不得用于商业用途,如转载请说明出处!
评论