如何解决用response输出字符流数据时的乱码问题

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取流对象之前,设置流的默认编码,ISO-8859-1设置为GBK
        resp.setCharacterEncoding("utf-8");
        //告诉浏览器服务器发送的消息体数据的编码,建议浏览器使用该编码解码
        resp.setHeader("content-type","text/html;charset=utf-8");
        //拿到一个response的PrintWiter对象,再向该对象的write方法将内容写到response的缓冲区,返回给浏览器
       PrintWriter pw= resp.getWriter();
       pw.write("对象");
        //乱码的原因:编码与解码用的码表不一致
        //reponse对象由tomcat实现,是拉丁文ISO-8859-1

    }

 

posted @ 2022-03-17 15:22  chaplu  阅读(335)  评论(0)    收藏  举报