request.getParameterMap() 转换为 Map

private static   Map parseQueryString(String queryString)
{
Map map=  new HashMap();
 
 
String[] params = queryString.split("&");  
    
    for (String param : params)  
    {  
        String name = param.split("=")[0];  
        String[] value = param.split("=")[1].split(",");  
         
        map.put(name, value);  
    }  
    return map;  
  
 
 
}

@SuppressWarnings("unchecked")
public static Map getQueryParams(HttpServletRequest request) {
Map map;
if (request.getMethod().equalsIgnoreCase("POST")) {
map = request.getParameterMap();
} else {
//为GET请求时就获取其请求参数的那段信息
String s = request.getQueryString();
if (StringUtils.isBlank(s)) {
return new HashMap();
}
try {
//将得到的HTML数据用UTF-8转码
s = URLDecoder.decode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
 
}
map = parseQueryString(s);
}


Map params = new HashMap(map.size());
int len;
for (Map.Entry entry : map.entrySet()) {
len = entry.getValue().length;
if (len == 1) {
params.put(entry.getKey(), entry.getValue()[0]);
} else if (len > 1) {
params.put(entry.getKey(), entry.getValue());
}
}
return params;
}

你可能感兴趣的:(request.getParameterMap() 转换为 Map)