java 转码 解决乱码

来源:互联网 发布:妈妈说就算域名在长廊 编辑:程序博客网 时间:2024/04/25 17:37

/*
编码转换,确保写数据库的时候不会出现乱码
*/

public class CodingConvert
{  
public CodingConvert()
{
  //
}
public String toGb(String uniStr){
     String gbStr = "";
     if(uniStr == null){
   uniStr = "";
     }
     try{
   byte[] tempByte = uniStr.getBytes("ISO8859_1");
   gbStr = new String(tempByte,"GB2312");
     }
  catch(Exception ex){
    }
     return gbStr;
}
  
public String toUni(String gbStr){
     String uniStr = "";
     if(gbStr == null){
   gbStr = "";
     }
     try{
   byte[] tempByte = gbStr.getBytes("GB2312");
   uniStr = new String(tempByte,"ISO8859_1");
     }catch(Exception ex){
    }
    return uniStr;
}
}