首页 > Web开发 > 详细

HttpServletResponse输出的中文乱码

时间:2015-09-18 23:14:18      阅读:306      评论:0      收藏:0      [点我收藏+]

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

 

HttpServletResponse输出的中文乱码

原文:http://www.cnblogs.com/liaojie970/p/4820437.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!