public class EncodeDemo {
    public static void main(String args[]) {
        //简单的办法
        String s1 = "\u6e38\u5ba2\u65e0\u6b64\u6743\u9650\uff01";
        String s2 = "\u7528\u6237\u4e0d\u5b58\u5728\uff01";
        String s3 = "\u6635\u79f0\u4e0d\u80fd\u4e3a\u7a7a\uff01";
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        
        //复杂的办法
        String s11 = "\\u6e38\\u5ba2\\u65e0\\u6b64\\u6743\\u9650\\uff01";
        String s12 = "\\u7528\\u6237\\u4e0d\\u5b58\\u5728\\uff01";
        String s13 = "\\u6635\\u79f0\\u4e0d\\u80fd\\u4e3a\\u7a7a\\uff01";
        System.out.println(encode(s11));
        System.out.println(encode(s12));
        System.out.println(encode(s13));
    }
    
    private static String encode(String s) {
        int pos = 0;
        StringBuffer sb = new StringBuffer();
        while (pos < s.length()) {
            switch (s.charAt(pos)) {
                case '\\' : {
                    if (s.charAt(pos + 1) == 'u'){
                        pos += 2;
                        char c = (char)Integer.parseInt(s.substring(pos, pos + 4), 16);
                        sb.append(c);
                        pos += 4;
                    } else {
                        sb.append('\\');
                        pos ++;
                    }
                    break;
                }
                default: {
                    sb.append(s.charAt(pos));
                    pos++;
                }
            }
        }
        return sb.toString();
    }
}

解决方案 »

  1.   

    我碰到个网页如下:
    <%@ page contentType="text/html; charset=UTF-8"%>
    <html>
    <head>
    <title>椤甸潰瑁呰浇</title>
    <table border="0" cellpadding="0" cellspacing="0" width="300">
        <tr>
          <td class='T1' height="25">
            <p align="center"><span class='bt'><font color='#ffffff'><B>鎿嶄綔缁撴灉</B></font></span></td>
        </tr>
        <tr>
          <td class='T2' align="center"><BR><span class='bt'>
            <font color='#ff0000'><bean:message key="<%=aForwardInfo.getMsg()%>"/></font></span><BR><BR>
          </td>
        </tr>
        <tr>
          <td class='T1' height="25">
            <p align="center">
    </body>
    </html>能把里面的那些汉字乱码转化为中文吗?