客户端通过HTTP POST多参数字符串java获取request参数 HttpServletRequest获取POST请求参数3种方法,比如(=&pwd=),获取到POST的参数有如下3种方法:
.()
<pre>private static String getPostData(HttpServletRequest request) {
StringBuffer data = new StringBuffer();
String line = null;
BufferedReader reader = null;
try {
<p>
reader = request.getReader();
while (null != (line = reader.readLine()))
data.append(line);
} catch (IOException e) {
} finally {
}
return data.toString();
}</pre></p>
获取到POST过来的信息(=&pwd=)java获取request参数,后面直接解析字符串就好了。
.()执行一次后(可正常读取body数据)java获取request参数,之后再执行就无效了。
@
<pre>@RequestMapping(value = "/testurl", method = RequestMethod.POST)
@ResponseBody
public ServiceResult TestUrl(HttpServletRequest request,
@RequestBody JSONObject jsonObject){
String username = jsonObject.get("username").toString();
String pwd = jsonObject.get("pwd").toString();
}
</pre>
@ 可以使用, Map ,或者绑定body。
@
<pre>@RequestMapping(value = "/testurl", method = RequestMethod.POST)
@ResponseBody
public ServiceResult TestUrl(HttpServletRequest request,@RequestParam("username")String username,
@RequestParam("pwd")String pwd) {
String txt = username + pwd;
}</pre>