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

  • 相关阅读:
    php上传excle文件,csv文件解析为二维数组
    transition的使用
    数组
    快捷键
    SCSS历史介绍与配置
    18-async函数
    this的指向问题
    媒体查询
    13-Set和Map数据结构
    15-Iterator和for…of循环
  • 原文地址:https://www.cnblogs.com/liaojie970/p/4820437.html
Copyright © 2011-2022 走看看