- /**
- * Get XML String of utf-8
- *
- * @return XML-Formed string
- */
- public static String getUTF8XMLString(String xml) {
- // A StringBuffer Object
- StringBuffer sb = new StringBuffer();
- sb.append(xml);
- String xmString = "";
- String xmlUTF8="";
- try {
- xmString = new String(sb.toString().getBytes("UTF-8"));
- xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");
- System.out.println("utf-8 编码:" + xmlUTF8) ;
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // return to String Formed
- return xmlUTF8;
- }
/**
* 字符串编码转换的实现方法
* @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;
}