- 发生此问题的前提条件:
数据库字符集为utf8
,并不是utf8mb4
的。
emoji图标插入utf8字符集的数据库时就会出现下面的错误
utf8占用3字节,而emoji占用4字节,所以会报错。
解决方案:
一、将数据库的字符集改为utf8mb4;
二、将emoji转义后存储:
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>4.0.0</version>
</dependency>
简单使用方法如下:
String str = "An :grinning:awesome :smiley:string 😄with a few :wink:emojis!";
String result = EmojiParser.parseToUnicode(str);
System.out.println(result);
// Prints:
// "An 😀awesome 😃string 😄with a few 😉emojis!"
String str = "An 😀awesome 😃string with a few 😉emojis!";
String result = EmojiParser.parseToAliases(str);
System.out.println(result);
// Prints:
// "An :grinning:awesome :smiley:string with a few :wink:emojis!"