表情字符转译存储及读取

package com.springboot.web;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springboot.entity.DbIdea;
import com.springboot.mapper.mysql.idea.DbIdeaDao;
import com.springboot.utils.IdGen;



@Controller
@RequestMapping("/idea")
public class IdeaController2 {

    @Autowired
    private DbIdeaDao dbIdeaDao;

    
    /**
     * 
     * 
     *   到获取意见的页面
     * @return
     */
    @RequestMapping("/home")
    public String home(Model model) {
    	DbIdea dbIdea = new DbIdea();
    	dbIdea.setId("51b4ef9d9bc543bbbb021b3960b41281");
    	DbIdea idea =dbIdeaDao.get(dbIdea);
    	try {
			idea.setExpectComment(emojiRecovery2(idea.getExpectComment()));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	model.addAttribute("idea", idea);
    	return "home";
    }
	
    
    /**
     * 保存
     * @return
     */
    @ResponseBody
    @RequestMapping("/doGetDbIdea")
    public String doGetDbIdea(DbIdea dbIdea) {
    	try {
    		
    		dbIdea.setExpectComment(emojiConvert1(dbIdea.getExpectComment()));//期望
    		
        	dbIdea.setId(IdGen.uuid());
			int i =dbIdeaDao.insert(dbIdea);
			return "true";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("评价失败");
			return "false";
		}
    	
    }
    
   
    
    /**
    * @Description 将字符串中的emoji表情转换成可以在utf-8字符集数据库中保存的格式(表情占4个字节,需要utf8mb4字符集)
    * @param str
    * 待转换字符串
    * @return 转换后字符串
    * @throws UnsupportedEncodingException
    * exception
    */
    public static String emojiConvert1(String str) throws UnsupportedEncodingException {
	    String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";
	
	    Pattern pattern = Pattern.compile(patternString);
	    Matcher matcher = pattern.matcher(str);
	    StringBuffer sb = new StringBuffer();
	    while(matcher.find()) {
		    try {
		    	matcher.appendReplacement( sb, "[[" + URLEncoder.encode(matcher.group(1), "UTF-8") + "]]");
		    } catch(UnsupportedEncodingException e) {
		    	throw e;
		    }
	    }
	    matcher.appendTail(sb);
	    	System.out.println("emojiConvert " + str + " to " + sb.toString()+ ", len:" + sb.length());
	    return sb.toString();
    }

    /**
    * @Description 还原utf8数据库中保存的含转换后emoji表情的字符串
    * @param str
	* 转换后的字符串
    * @return 转换前的字符串
    * @throws UnsupportedEncodingException
    * exception
    */
    public static String emojiRecovery2(String str) throws UnsupportedEncodingException {
	    String patternString = "\\[\\[(.*?)\\]\\]";
	
	    Pattern pattern = Pattern.compile(patternString);
	    Matcher matcher = pattern.matcher(str);
	
	    StringBuffer sb = new StringBuffer();
	    while(matcher.find()) {
		    try {
			    matcher.appendReplacement(sb,URLDecoder.decode(matcher.group(1), "UTF-8"));
		    } catch(UnsupportedEncodingException e) {
		    	throw e;
		    }
	    }
	    matcher.appendTail(sb);
	    System.out.println("emojiRecovery " + str + " to " + sb.toString());
	    return sb.toString();
    }
    
}

猜你喜欢

转载自blog.csdn.net/u014596302/article/details/86604551