最近用到了字符串的压缩,用到了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的转换。
|