JavaEE中request.getParameterMap()转普通Map
在java web项目中虽然可以通过request.getParameterMap()很轻松的获得参数Map,但得到的Map和普通Map是不一样的,是被锁定的,不能像操作常规Map那样进行put、get等操作,该方法将得到参数Map返回为可操作的普通Map
1 /** 2 * 从request中获得参数Map,并返回可读的Map 3 * 4 * @param request 5 * @return 6 */ 7 @SuppressWarnings("unchecked") 8 public static Map getParameterMap(HttpServletRequest request) { 9 // 参数Map 10 Map properties = request.getParameterMap(); 11 // 返回值Map 12 Map returnMap = new HashMap(); 13 Iterator entries = properties.entrySet().iterator(); 14 Map.Entry entry; 15 String name = ""; 16 String value = ""; 17 while (entries.hasNext()) { 18 entry = (Map.Entry) entries.next(); 19 name = (String) entry.getKey(); 20 Object valueObj = entry.getValue(); 21 if(null == valueObj){ 22 value = ""; 23 }else if(valueObj instanceof String[]){ 24 String[] values = (String[])valueObj; 25 for(int i=0;i<values.length;i++){ 26 value = values[i] + ","; 27 } 28 value = value.substring(0, value.length()-1); 29 }else{ 30 value = valueObj.toString(); 31 } 32 returnMap.put(name, value); 33 } 34 return returnMap; 35 }