/** * 字符串編碼轉換的實現方法 * @param str 待轉換編碼的字符串 * @param newCharset 目標編碼 * @return * @throws UnsupportedEncodingException */ public String changeCharset(String str, String newCharset) throws UnsupportedEncodingException { if (str != null) { //用默認字符編碼解碼字符串。 byte[] bs = str.getBytes(); //用新的字符編碼生成字符串 return new String(bs, newCharset); } return null; } /** * 字符串編碼轉換的實現方法 * @param str 待轉換編碼的字符串 * @param oldCharset 原編碼 * @param newCharset 目標編碼 * @return * @throws UnsupportedEncodingException */ public String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException { if (str != null) { //用舊的字符編碼解碼字符串。解碼可能會出現異常。 byte[] bs = str.getBytes(oldCharset); //用新的字符編碼生成字符串 return new String(bs, newCharset); } return null; }