今天使用request.getParameterMap()获得Map中的数据时,使用
Map map=hrequest.getParameterMap();
Set key = map.keySet();
for(Object aaa: key.toArray()){
parakey = aaa.toString();
paravalue = (String)map.get(aaa);
requestpath = requestpath+"?"+parakey+"="+paravalue;
}
Map map=hrequest.getParameterMap();
Set key = map.keySet();
for(Object aaa: key.toArray()){
parakey = aaa.toString();
paravalue = (String)map.get(aaa);
requestpath = requestpath+"?"+parakey+"="+paravalue;
}
报[Ljava.lang.String; cannot be cast to java.lang.String错误,上网查了才知道get(key)返回的是String[],所以要使用字符串数组转换读取,参见英文接口文档:
所以可以创建工具类转化为普通map:
/**
* 从request中获得参数Map,并返回可读的Map
*
* @param request
* @return
*/
@SuppressWarnings("unchecked")
public static Map getParameterMap(HttpServletRequest request) {
// 参数Map
Map properties = request.getParameterMap();
// 返回值Map
Map returnMap = new HashMap();
Iterator entries = properties.entrySet().iterator();
Map.Entry entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if(null == valueObj){
value = "";
}else if(valueObj instanceof String[]){
String[] values = (String[])valueObj;
for(int i=0;i<values.length;i++){
value = values[i] + ",";
}
value = value.substring(0, value.length()-1);
}else{
value = valueObj.toString();
}
returnMap.put(name, value);
}
return returnMap;
}