最近用到了字符串的压缩,用到了gzipinputstream和gzipoutputstream,再次遇到了阔别已久的中文乱码问题。
看了一些相关的文章,觉得我们之所以会遇到这样那样的乱码问题,基本上都是由于我们在某些地方隐含了byte到char的转换,而这种隐含的转换采用的是iso-8859-1的编码进行的。
以jsp页面中文传递为例子,假设客户端的编码是gb2312,表单中的中文提交后,首先根据gb2312编码转换为字节流,到达服务器端后,如果我们直接在servlet中调用request.getparameter(string name)等方法,由于方法返回的是string 对象,所以其中必然隐含了一次从byte到char的转换,错误也就是在这里产生的,如果这次转换采用的编码是iso-8859-1,得到的当然是乱码。
public class login
extends httpservlet {
private static final string content_type = "text/html; charset=utf-8";
.....
//initialize global variables
public void init() throws servletexception {
}
//process the http get request
public void doget(httpservletrequest request, httpservletresponse response) throws
servletexception, ioexception {
string name = request.getparameter("userid");//隐含的转换
name = new string(name.getbytes("iso-8859-1"), "gb2312");//还原字节,重新构造
response.setcontenttype(content_type);
printwriter out = response.getwriter();
out.println("<html>");
out.println("<head><title>login</title></head>");
out.println("<body bgcolor=/"#ffffff/">");
out.println("<p>the servlet has received a get. this is the reply.</p>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
幸好,以iso-8859-1进行的默认转换不会损失字节,也不会增加字节,我们只要按照iso-8859-1的方式返回原来的字节数组,重新按照gb2312的方式进行byte 到char的转换就可以了。
再以压缩流为例(文件流实际上也是一样的)
public string uncompress(byte[] cmp) {
string ret = "";
int i;
byte[] buf = new byte[512];
try {
/**
*新的方式,始终保持以字节为核心,最后再按照合适的编码进行组装
*/
bufferedinputstream bis = new bufferedinputstream(new gzipinputstream(new
bytearrayinputstream(cmp)));
/**
* 以前的方式
* 在 new inputstreamreader()的时候发生了隐含的byte到char的转换,导致之后出来的都是乱码
*/
// bufferedreader bis = new bufferedreader(new inputstreamreader(new
// gzipinputstream(new
// bytearrayinputstream(cmp))));
bytearrayoutputstream baos = new bytearrayoutputstream();
bufferedoutputstream bos = new bufferedoutputstream(baos);
while ( (i = bis.read(buf)) > 0) {
bos.write(buf, 0, i);
}
bos.close();
baos.close();
bis.close();
ret = new string(baos.tobytearray());//用平台默认的编码进行组装,我是gb2312
}
catch (ioexception ex) {
ex.printstacktrace();
}
return ret;
}
reader是以字符为核心,inputstream是以byte为核心的,当他们转换的时候就会进行byte到char的转换,所以我们要注意自己的调用的顺序。
我们如果今后再遇到乱码的问题,就去找找自己是不是什么地方进行了隐含的byte到char的转换。
闽公网安备 35060202000074号