中文乱码问题
1. 出现乱码的原因
1) 在计算机中数据以二进制的形式进行存储的, 数据的传输也是通二进制的形式
2)需要存字符,会出现字符与字节之间的转换 (输入字符 读到的字符)
3) 字符和字节之间如何实现转换? 都是通过查码表
4) 字符到字节是编码,字节到字符是解码, 编码和解码用到了不同码表就会出现乱码问题
我们把字符到字节称为编码,字节到字符称为解码
即字符--->编码--->字节--->解码--->字符
如下例子解码编码一致
public class Demo01 { public static void main(String[] args) throws UnsupportedEncodingException { //字符--->编码--->字节--->解码--->字符 String str = "中国"; System.out.println(str); //编码 byte[] bytes=str.getBytes(); //解码 String result=new String(bytes); String result1=new String(bytes,"utf-8"); System.out.println(result); System.out.println(result1); } }
2. 出现乱码的情况,编码错和解码错
1):解码错,
解码错 ,用错误的码表编码再用正确的码表解码可以解决
package com.bjsxt.web; import java.io.UnsupportedEncodingException; public class Demo02 { public static void main(String[] args) throws UnsupportedEncodingException { //字符--->编码--->字节--->解码--->字符 String str = "中国"; System.out.println(str); //编码 byte[] bytes=str.getBytes("utf-8"); //解码错 String result=new String(bytes,"gbk"); // 遇到乱码 解码错 用错误的码表编码 再用正确的码表解码 result=new String(result.getBytes("gbk"),"utf-8"); System.out.println(result); } }
2):编码错
编码错无药可救
package com.bjsxt.web; import java.io.UnsupportedEncodingException; public class Demo03 { /** * 编码错 无药可救 */ public static void main(String[] args) throws UnsupportedEncodingException { String data = "你好"; // 编码 byte[] bytes = data.getBytes("iso8859-1"); for (byte b : bytes) { System.out.println(b); } // 解码 String result = new String(bytes, "utf-8"); System.out.println(result); } }
3.Response对象乱码问题
1). response getOutputStream 只需要指定浏览器的解码方式(response.setHeader("content-type", "text/html;charset=utf-8")),即通知浏览器使用 utf-8编码(实际上是通知解码方式)
package com.bjsxt.web; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ResponseDemo02 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String str = "中国"; // 指定浏览器的解码方式 // 通知浏览器 使用 utf-8 编码 response.setHeader("content-type", "text/html;charset=utf-8"); //获得输出流 OutputStream out = response.getOutputStream(); //编码 发送响应体 out.write(str.getBytes("utf-8")); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2). response getWriter方法获得字符流,用于向浏览器输出字符数据
需要指定response编码方式(response.setCharacterEncoding("utf-8")),又需要指定浏览器解码方式(response.setHeader("content-type", "text/html;charset=utf-8")),由于这两个太常用了,于是tomcat把他们封装了成response.setContentType("text/html;charset=utf-8");
package com.bjsxt.web; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ResponseDemo03 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "你好"; // 指定response的编码 //response.setCharacterEncoding("utf-8"); // 指定浏览器的解码方式 // 通知浏览器 使用 utf-8 编码 // response.setHeader("content-type", "text/html;charset=utf-8"); // 解决response 乱码 必须要位于第一次 调用 getWriter 之前 response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); // new PrintWriter(outputstream, "utf-8") out.print("abcd"); //此处需先编码 out.print(data); //response.getOutputStream().write("abcd".getBytes("gbk")); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3.Request对象请求参数乱码问题
request.setCharacterEncoding("utf-8"); // post方式一消息体发送 ,只对post方式有效 对消息头无效
package com.bjsxt.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RequestDemo04 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解码错 //request以ios8859-1编码了 String name = request.getParameter("name"); //手动以ios8859-1解码 byte[] bytes = name.getBytes("iso8859-1"); //再以utf-8编码 name = new String(bytes,"utf-8"); System.out.println("name=" + name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解码错 // 设置request的编码 // request.setCharacterEncoding("utf-8"); // post方式一消息体发送 ,只对post方式有效 对消息头无效 // 获得请求参数 request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); System.out.println("name=" + name); } }

浙公网安备 33010602011771号