java storage emoji expression solution

1. The problem occurs
    . Some useful avatars use emoji expressions when developing IM or emoji expressions are used when chatting. However, the encoding used in the mysql database is utf8_general_ci, which is utf-8 encoding, and the result is an error. The emoji cannot be stored in the database.

    java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\xAA",...' for column 'raw_json' at row 1

2. Why does this happen
because mysql's utf8 encodes one character at most 3 bytes, but an emoji expression is 4 bytes, so utf8 does not support storing emoji expressions. But utf8mb4, a superset of utf8, can have up to 4 bytes per character, so it can support the storage of emoji expressions.

3. Solution

    Option 1: Modify the encoding set of the database to utf8mb4, whether it is a database, a table, or a field. Although it will increase the storage, this can be ignored.

   Option 2: Sometimes the database encoding cannot be modified for some reason, you can use java to Convert and encode emoji expressions, such as emoji-java, a jar that handles emoji expressions specially, and then save or remove the expressions, which is also a compromise solution.

  Download address of emoji-java: https://github .com/vdurmont/emoji-java Following

me is my biggest motivation


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326399969&siteId=291194637