zoukankan      html  css  js  c++  java
  • PrintWriter 输出信息乱码

      异步方式,返回json给前台时,向前台输出信息使用PrintWriter,但是在输出的过程中,出现乱码的情况。

    于是我想起来response.setCharacterEncoding("utf-8");设置页面编码,以及response.setContentType("text/html; charset=utf-8");设置内容类型编码,但是在实验后不成功,乱码依旧。

        PrintWriter out = response.getWriter();
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        out.print(json);
        out.flush();
        out.close();
    

     返回的json如下:

    {"seriesData":[22619,22671,21908,5415,0],"caption":"组织机构代码-年度统计","xAxisData":["2010年度","2011年度","2012年度","2013年度","2014年度"]}
    

     经检查,发现PrintWriter会先获取项目的 编码,根据编码来自己设定characterEncoding,所以得在获取这个PrintWriter对象之前设置编码。如下:注意顺序。

      
    response.setCharacterEncoding("utf-8");
      response.setContentType("text/html; charset=utf-8");
      PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
        out.close();
    
  • 相关阅读:
    Python float() 函数
    Python bytearray() 函数
    scanner.nextInt()与scanner.nextDouble
    Scanner对象next与nextLine
    JavaDoc
    包机制
    运算符要点
    变量与常量
    test
    类型转换
  • 原文地址:https://www.cnblogs.com/lucky2u/p/3807931.html
Copyright © 2011-2022 走看看