中文解码Unicode

package com.j1.search.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.lang.StringUtils;

public class WebUtils
{
    public static String convertUnicode(String unicodeStr) throws UnsupportedEncodingException
    {
        if(StringUtils.isNotBlank(unicodeStr) && unicodeStr.startsWith("%"))
        {
            while(unicodeStr.contains("%25"))
            {
                unicodeStr = unicodeStr.replace("%25", "%");
            }
            
            if(unicodeStr.length() == 3)
            {
                unicodeStr = URLDecoder.decode(unicodeStr, "utf-8");
            }
            else
            {
                boolean flag = true;
                for(int i = -1; i < 10 && (i + 3) < unicodeStr.length(); i += 3)
                {
                    if(unicodeStr.indexOf("%", i) != (i + 1))
                        flag = false;
                }
                if(flag)
                {
                    unicodeStr = URLDecoder.decode(unicodeStr, "utf-8");
                }
            }
            return unicodeStr;
        }
        else
        {
            return unicodeStr;
        }
    }
    
    public static String convertGetParams(String isoStr) throws UnsupportedEncodingException
    {
        if(StringUtils.isNotBlank(isoStr))
        {
            return new String(isoStr.getBytes("iso8859-1"),"UTF-8");
        }
        else
        {
            return isoStr;
        }
    }
    
    public static void main(String[] args) throws Exception
    {
        String aa = "%E6%AC%A7%E5%A7%86%E9%BE%99%E8%A1%80%E5%8E%8B%E8%AE%A1";// 给个默认值
        System.out.println(convertUnicode(aa));
    }
    
}

 

posted @ 2017-12-01 09:05  小啊菜鸡  阅读(373)  评论(0编辑  收藏  举报