当客户端提交数据后台来解析-IO

数据解析,保存遗产!强大内涵

一些看不懂的数据首先要确定他的编码格式,其次才能准确作出解析,可以拿一些数据,我博客里有一个小方法来判断是什么可是!!

BufferedReader是继承于Reader对象,从字符输入流中读取文本,缓冲各个字符,从而提供字符,数组和行的高效读取.

private String getRequestBody(HttpServletRequest request) throws  IOException{
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(
                        inputStream,"utf-8"));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }
        }
        String body = stringBuilder.toString();
        log.debug("get card back body:"+body);
        return body;
    }

相关推荐