判断一个字符串的编码格式,并开展转码

判断一个字符串的编码格式,并进行转码
public class TranCharset {
    /**
     * 判断字符串的编码
     *
     * @param str
     * @return
     */
    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        return "";
    }
    public static void main(String[] args){
    	System.out.println(getEncoding("CSS测试"));
    }
}
public static void main2(String[] args) throws Exception{
        String desc="CSS测试"; 
        try { 
           String b = new String(desc.getBytes("GBK"), "UTF-8");
           System.out.println(b);
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 

    }
}
1 楼 kong0itey 2011-08-24  
判断一个字符串的编码格式,并开展转码 判断一个字符串的编码格式,并开展转码 判断一个字符串的编码格式,并开展转码 判断一个字符串的编码格式,并开展转码
判断一个字符串的编码格式,并开展转码 判断一个字符串的编码格式,并开展转码 判断一个字符串的编码格式,并开展转码 判断一个字符串的编码格式,并开展转码 判断一个字符串的编码格式,并开展转码
2 楼 kong0itey 2011-08-24  
:  : :id判断一个字符串的编码格式,并开展转码   8 :c判断一个字符串的编码格式,并开展转码 ry: )判断一个字符串的编码格式,并开展转码 ea: ?  :判断一个字符串的编码格式,并开展转码 (判断一个字符串的编码格式,并开展转码 P
3 楼 kong0itey 2011-08-24  
发给
会尽快结婚 回过
4 楼 kong0itey 2011-08-24  
[i[mgx]][//img]
5 楼 kong0itey 2011-08-24  
该发言已被管理员删除