那些年我们一起"日过的"乱码

    上一篇博客,介绍了流的使用,本节来说一下乱码的处理。数据流到了一处,你怎样解码,或者怎么编码,遵照什么规范,就会涉及到乱码的情况。

     计算机底层是用二进制来存储数据的,

     1、首先我们先不考虑服务器浏览器的情况,只考虑本地

        <1>

             //解码byte-char
		String str="中国";///使用eclipse平台默认(GBK)
		//编码,char到byte
		byte[] data=str.getBytes();//使用eclipse平台默认(GBK)
		
		//解码
		System.out.println(new String(data));//输出中国:编码与解码一致

 

      <2>编码用了utf-8,解码却使用默认的编码(GBK)

                 data=str.getBytes("utf-8");
		
		System.out.println(new String(data));//输出乱码,不一致

      涓浗

      <3>

	data=str.getBytes("utf-8");
		
		System.out.println(new String(data,"utf-8"));

       输出:中国

      <4>

扫描二维码关注公众号,回复: 374238 查看本文章

     

String str="中国";
		System.out.println(str.length());
		byte[] data=str.getBytes();
		System.out.println(new String(data,0,str.length()-1));//字节数据缺失,乱码

        输出:?

        

     <5>、补充一下字节流和字符流的转化InputStreamReader(InputStream),OutputStreamWriter(OutputStream)

    

		BufferedReader br=null;
		br=new BufferedReader(new InputStreamReader(new FileInputStream(new File("F:/xp/test/Demo1.java")),"utf-8"));
		String str=null;
		BufferedWriter bw=new BufferedWriter(new FileWriter(new File("F:/xp/test/Demo11.txt")));
		while(null!=(str=br.readLine())){
			System.out.println(str);
			bw.write(str);
			bw.newLine();
		}
		bw.close();
		br.close();

    把一个文件的字符拷贝到另外的一个文件。

    2、网络情况下的乱码

     新建一个Servlet

public class EncodingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public EncodingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String meg="中国";
		OutputStream osOutputStream=response.getOutputStream();
		osOutputStream.write(meg.getBytes("utf-8"));
		//为了全球通用设为utf-8
	}

	 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

 

  serlvet写给浏览器的98 99(utf-8).不告诉浏览器,用GBK查98 99(GBK) 是 涓浗。

改正方式1:改浏览器的码表(不好,用户可没这么聪明)

改正方式二:response设定编码方式:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
                response.setHeander("content-type","text/html:utf-8");//浏览器打开方式
		String meg="中国";
		OutputStream osOutputStream=response.getOutputStream();
		osOutputStream.write(meg.getBytes("utf-8"));//程序输出方式
		//为了全球通用设为utf-8
	}

 

程序以什么码表输出了,就一定要用什么码表打开。

改正方式三:jsp里有这个标签:<meta http-equiv="Content-Type" content="text/html; charset=utf-8">来设定编码,我们可以把这个标签来写给浏览器,来让浏览器来解析这个东西,知道怎么编码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String meg="中国";
		OutputStream osOutputStream=response.getOutputStream();
                 osOutputStream.write("meta http-equiv="Content-Type" content="text/html;cha
                   rset="utf-8"
".getbytes());
		osOutputStream.write(meg.getBytes("utf-8"));
		//为了全球通用设为utf-8
	}

      不用http协议发送header,用html的meta标签模拟一个http响应头。

3、还有一个经典的问题:在ServletdoGet方法里

OutputStream osOutputStream=response.getOutputStream();
		osOutputStream.write(1));

  任然会得不到1,是因为,1是个int型数字,不涉及到编码,而浏览器还傻不拉唧的去找1对应的汉字,就得到的不是1了。

如果这样改

OutputStream osOutputStream=response.getOutputStream();
		osOutputStream.write("1");

 就能输出1了,输出之前会编码

以上就是乱码的一些情况,与君共勉

    

猜你喜欢

转载自1509930816.iteye.com/blog/2246241