emoji表情,直接存入資料庫,出現java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E\xF0\x9F...' for.
阿新 • • 發佈:2018-12-06
最近專案中在做基於netty-socketio的web實時聊天功能,剛做好可以建群、發訊息,李老闆說要試一下功能,老闆第一條訊息就發了一個表情,當場導致後臺出現一大片紅,,,[笑哭]
問題:使用者傳送的emoji表情,直接存入資料庫,會出現java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x98\x8E\xF0\x9F…’ for…
原因:emoji表情是utf-8編碼,但是佔用4個位元組,而mysql的utf-8字符集的資料庫每個字元只有3個位元組,所以無法儲存emoji表情到mysql資料庫。
解決:通過第三方jar包來轉換emoji表情,加入maven依賴:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>java-emoji-converter</artifactId>
<version>0.1.1</version>
</dependency>
程式碼中,對聊天的內容進行轉義:
private EmojiConverter emojiConverter = EmojiConverter.getInstance();
emojiConverter.toAlias (data.getMsgContent());//將聊天內容進行轉義
下面的參考連結中,還給出了另外兩種方法,當然這個方法是最完美的解決方案。
參考:https://blog.csdn.net/u012873407/article/details/79072536?>=