protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String requestString = getRequestPayload(request);
Map<String, String> requestMap = new HashMap<String, String>();
requestMap = RequestInfoUtil.requestUtil(requestString);
String requestMethod = requestMap.get("aaa");
}
private String getRequestPayload(HttpServletRequest req)
{
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = req.getReader();)
{
char[] buff = new char[1024];
int len;
while ((len = reader.read(buff)) != -1)
{
sb.append(buff, 0, len);
}
}
catch (IOException e)
{
e.printStackTrace();
}
return sb.toString();
public class RequestInfoUtil
{
public static Map<String, String> requestUtil(String request)
{
Map<String, String> map = new HashMap<String, String>();
strRequest(map, request);
return map;
}
public static String strRequest(Map<String, String> map, String s)
{
int length = s.length();
int index1 = s.indexOf("=");
String parm1 = s.substring(0, index1);
int index2 = s.indexOf("&");
if (index2 == -1)
{
String parm2 = s.substring(index1 + 1);
map.put(parm1, parm2);
return null;
}
String parm2 = s.substring(index1 + 1, index2);
map.put(parm1, parm2);
return strRequest(map, s.substring(index2 + 1));
}
}
post请求request为流时,转化为map
最新推荐文章于 2024-07-27 22:52:03 发布
本文介绍了一个HTTP请求处理的方法,包括如何从HttpServletRequest中获取请求负载,并将其转换为字符串形式,然后进一步解析成Map结构。重点讲解了POST请求的数据读取及解析过程。
553

被折叠的 条评论
为什么被折叠?



