zoukankan      html  css  js  c++  java
  • Mysql数据库存储emoji表情

    emoji表情需要使用编码格式为utf8mb4,mysql数据库版本要5.5以上,我用的是5.6,因为只有5.5以上支持utf8mb4。

    1.数据库编码设定为utf8mb4,如果建库时指定的是utf8,则需要执行语句:ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

    2.同时指定数据库配置参数中character_set_server改为utf8mb4

    3.把对应的需要保存emoji表情的字段编码设置为utf8mb4_unicode_ci

    4.配置完成,则可完美存储emoji表情了。

    后续

    最近发现很多新增的微信表情即使是使用了utf8mb4_unicode_ci编码也无法存储,所以索性直接使用base64加密后再存储,读取的时候解密即可,这样就不会在遇到这个令人头疼的问题了,加密解密算法如下:

    注:解密算法我这边做了一些优化(因为之前数据表中存在一些未加密数据,直接解密会报错)

    public static String base64Encode(String str){
            String result="";
            try {
                result = new String (Base64.getEncoder().encode(str.getBytes("utf-8")),"utf-8");
            } catch (Exception e) {
                e.printStackTrace();
                result="";
            }
            System.out.println(result);
            return result;
        }
        public static String base64Decode(String str){
            String result="";
            String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
            if(Pattern.matches(base64Pattern, str)){
                try {
                    result = new String (Base64.getDecoder().decode(str.getBytes("utf-8")),"utf-8");
                    String badChar="�";
                    if(result.contains(badChar)){
                        result=str;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result=str;
                }
            }else {
                result=checkNull(str);
            }
            //System.out.println(result);
            return result;
        }
  • 相关阅读:
    EVRYTHNG.H
    关于轮胎尺寸问题
    常见内核数据结构.doc
    i5处理器的台式机[百度知道]
    debug和release版区别
    booklist 转
    windows 系统编程 Chap7 线程和调度
    一个超级简单的dwr配置文件,介绍了dwr最常用的几个标签(转)
    用凭据管理器提升Windows7访问速度(非原创)
    IEC87005104 传输规约(国电)
  • 原文地址:https://www.cnblogs.com/kui-technology/p/7058169.html
Copyright © 2011-2022 走看看