JAVA 字符编码小结

 这一段时间在开发过程中遇到这样一个问题。相同的字符串用http协议访问时正常显示,而采用https协议访问时则出现乱码。采用new String(s_fullname.getBytes("iso-8859-1"),"gb2312")的方式进行转化后,在https下正常显示,但是在http方式下又出现乱码,并且两次出现的乱码不同。解决方式如下:

byte[]temp1,temp2;  
   temp1 = s_fullname.getBytes("iso-8859-1");  
   temp2 = s_fullname.getBytes("gb2312");  
   String   compstring1,compstring2;  
   compstring1   =   new   String(temp1);  
   compstring2   =   new   String(temp2);  
   String ss_fullname = "";
   if(s_fullname.equals(compstring2)){  
    ss_fullname = String.valueOf(s_fullname);
   }else{
     ss_fullname =  new String(s_fullname.getBytes("iso-8859-1"),"gb2312");
   }

网上找到的一些小知识:

String str="英";
//取得GB2312编码的字节
byte[] bytesGB2312=str.getBytes(GB2312");

//取得平台确省编码的字节(solaris为ISO8859-1,windows为GB2312)
byte[] bytesDefault=str.getBytes();

//用指定的编码将字节转换成字符串
String newStrGB=new String(byteDefault);

//用指定的编码从字节流里面读取字符
InputStream in=xxx;
InputStreamReader reader=InputStreamReader(in,"GB2312");
char aChar=reader.read();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值