zoukankan      html  css  js  c++  java
  • HttpServletResponse输出的中文乱码

    HttpServletResponse输出有两种格式,一种是字符流,一种是字节流。

    1.字符流

         // 这句话的意思,是让浏览器用utf8来解析返回的数据,即设置客户端解析的编码
            response.setContentType("text/html; chartset=UTF-8");
            //这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859 ,即服务端对中文的编码 
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("中文");

    2.字节流

         // 这句话的意思,是让浏览器用utf8来解析返回的数据,即设置客户端解析的编码
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            // response.setContentType("text/html; chartset=UTF-8");  //尝试使用这个设置“Content-type”未成功
            String data = "中文";
            OutputStream ps = response.getOutputStream();
            // 这句话的意思,使得放入流的数据是utf8格式
            ps.write(data.getBytes("utf-8"));

    参考:

    http://blog.csdn.net/kontrol/article/details/7767983

  • 相关阅读:
    植物:柏树
    植物:水杉
    植物:珙桐
    植物:桫椤
    汉语-成语:悠闲自在
    植物:孑遗植物
    汉语-词语:孑遗
    汉语-词语:调味品
    调味品:酱油
    netstat 命令详解
  • 原文地址:https://www.cnblogs.com/liaojie970/p/4820437.html
Copyright © 2011-2022 走看看