java request.getInputStream中文乱码解决方案

请求时要指定为UTF-8,中文码码完美解决

/**
     *
     * 得到请求body字符串,一般用于content-type:application/json
     *
     */
    public static String getBodyString(HttpServletRequest request) {
        StringBuilder buffer = new StringBuilder("");
        try{
            request.setCharacterEncoding("UTF-8");
            BufferedReader reader=null;
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
            String line=null;
            while((line = reader.readLine())!=null){
                buffer.append(line);
            }
        }catch (IOException e) {
            LogerHelper.WebLog(e);
        }
        return buffer.toString();
    }

 

posted @ 2019-09-05 11:54  为乐而来  阅读(4077)  评论(0编辑  收藏  举报