背景
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); }