获取HTTP字符串正文
String getBodytxt(HttpServletRequest request) { BufferedReader br = request.getReader(); String str, wholeStr = ""; while((str = br.readLine()) != null){ wholeStr += str; } return wholeStr; }
获取HTTP二进制正文
private String getBodyData(HttpServletRequest request) { StringBuffer data = new StringBuffer(); String line = null; BufferedReader reader = null; try { reader = request.getReader(); while (null != (line = reader.readLine())) data.append(line); } catch (IOException e) { } finally { } return data.toString(); }
使用spring注解获取(传过来的内容必须要符合json格式才行)
@PostMapping("/test")
@ResponseBody
public String test(HttpServletRequest httpServletRequest, @RequestParam JSONObject jsonObject){
String username = jsonObject.get("username").toString(); String pwd = jsonObject.get("pwd").toString(); }
获取Param传过来的参数(xxx?name=xxx&age=xxx)(也可以不用这种方法,因为servlet中有直接的方法获取Param值)
@RequestMapping(value = "/testurl", method = RequestMethod.POST) @ResponseBody public ServiceResult TestUrl(HttpServletRequest request,@RequestParam("username")String username, @RequestParam("pwd")String pwd) { String txt = username + pwd; }
通过第一种和第二种方法获取的字符串会有反斜杠可以通过阿里巴巴的json插件去除反斜杠
JSONObject jsonObject = (JSONObject) JSON.parse(String.valueOf(data));(data是获取的正文字符串)
原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/268069.html