首页 > 编程 > Java > 正文

Java String 编码格式转换

2019-11-11 06:57:18
字体:
来源:转载
供稿:网友
1.TODO 目前使用ISO8859-1编码作为输入! PRivate String convertEncoding(String requestxml) {     String temp = "";     try {          temp = new String(requestXML.getBytes("ISO8859-1"), "UTF-8");     } catch (UnsupportedEncodingException e) {          Assert.state(false, e.getMessage());     }    return temp; } 

==============================================================

//工具类

public class ConvertToolUtils {     public static String gbk2iso(String src) {          return convertEncoding(src, "GBK", "ISO-8859-1");     }     public static String iso2gbk(String src) {         return convertEncoding(src, "ISO-8859-1", "GBK");    }    public static String convertEncoding(String source, String sourceEncoding, String targetEncoding) {        try {             if (source == null || source.equals("")) {                  return "";             } else {                   return new String(source.getBytes(sourceEncoding), targetEncoding);             }          } catch (Exception e) {              return "";          }   }   public static int ConvertStrNum2IntNum(String strNum) {        int Num = 0;        try {             if (null == strNum || strNum.trim().equals("")) {                  Num = 0;             } else {                   Num = Integer.parseInt(strNum);             } 

         } catch (Exception e) { 

                Num = 0;                 e.printStackTrace();          }       return Num;    } } 使用方式: 

requestXML = ConvertToolUtils.convertEncoding(requestXML,"ISO8859-1","UTF-8");

=======================================

 public String getUTF8XMLString(String xml) {// A StringBuffer ObjectStringBuffer 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");//加码//java.net.URLDecoder.decode(str,"UTF-8");//解码System.out.println("utf-8 编码:" + xmlUTF8);} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}// return to String Formedreturn xmlUTF8;}  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表