Java HttpServletResponse reponse中文到页面乱码

   
 /**
     * Print string to page
     * ------------------------
     *
     * @param str      The print string
     * @param response The HttpServletResponse
     */
    public static void printStringToPage(String str, HttpServletResponse response) {
        PrintWriter out = null;
        try {
            if(null != response){
                  // 设置contentType,别漏掉"text/html;"
                response.setContentType("text/html;charset=UTF-8");
                out = response.getWriter();
                out.print(str);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(null != out){
                out.close();
                out = null;
            }
        }

    }


你可能感兴趣的:(Java HttpServletResponse reponse中文到页面乱码)