java ipone WeChat nickname emoji expression failed to save and cannot be stored in the database (1)

Transfer: http://blog.csdn.net/truong/article/details/46891525

 

Description of the problem: There is a problem in saving the database by entering "emoji expression" in the nickname of ipone5s.

 

Operating environment: Java, Tomcat, mysql, Linux (garbled characters under my Linux, this is a Linux problem)

 

 

 

Reason: emoji expressions of IOS 5.x and above are all 4 bytes, and utf8 in mysql has a maximum of 3 bytes per character. Therefore, if the database cannot be saved, the following error will be reported:

 

Caused by: java.sql.SQLException: Incorrectstring value: '\xF0\x9F\x8E\x80\xE5\xA4...' for column 'nickname' at row 1

 

 

 

Solution:

 

1. Remove emoji (this method is used)

 

2. Modify the Mysql data encoding. Now the utf8mb4 encoding in MySQL is extended to a maximum of 4 bytes per character (the existing database encoding needs to be modified, and this method is not used).

 

 

 

the first method:

 

"Remove emoji, java class:

 

"Call code: EmojiFilter.filterEmoji("nickname");

 

 

 

 

code

 

 

public class EmojiFilter {


    /**
     * Check if there are emoji characters
     * @param source
     * @return throws if it contains
     */
    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, it is judged here to indicate , confirm that there are emoji characters
                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) {
        
        if (!containsEmoji(source)) {
            return source;//If not included, return directly
        }
        //It must include
        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 {
            }
        }
        
        if (buf == null) {
            return source;//If no emoji is found, return the source string
        } else {
            if (buf.length() == len) {//The meaning here is to have as little toString as possible, because the string will be regenerated
                buf = null;
                return source;
            } else {
                return buf.toString();
            }
        }
        
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326643141&siteId=291194637