HttpServletRequest中读取HTTP请求的body

背景

Spring MVC 等框架用久了之后,发现对于POST请求里面如果不使用@RequestBody 好像很难获得POST的body。

HTTP请求中的是字符串数据

字符串读取

void charReader(HttpServletRequest request) {
    BufferedReader br = request.getReader();

    String str, wholeStr = "";
    while((str = br.readLine()) != null){
        wholeStr += str;
}
System.out.println(wholeStr);

}

 

二进制读取

void binaryReader(HttpServletRequest request) {
        int len = request.getContentLength();
        ServletInputStream iii = request.getInputStream();
        byte[] buffer = new byte[len];
        iii.read(buffer, 0, len);
}

 

void binaryReader(HttpServletRequest request) { intlen = request.getContentLength(); ServletInputStream iii = request.getInputStream(); byte[] buffer = new byte[len]; iii.read(buffer, 0, len); }

上一篇:常见问题--post发送参数使用httpservletrequest读取为空


下一篇:解决HttpServletRequest的输入流只能读取一次的问题