这一段时间在开发过程中遇到这样一个问题。相同的字符串用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();