request.getParameterMap值[Ljava.lang.String; cannot be cast to java.lang.String错误

When using request.getParameterMap() to get the data in the Map today, use
       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;
            }

Cannot be cast to java.lang.String error, I checked the Internet and found out that get(key) returns String[], so use string array conversion to read, see the English interface documentation:
 

So you can create a tool class and convert it into a normal map:

[java] view plain copy

  1. /** 
  2.  * Get the parameter Map from the request and return a readable Map 
  3.  *  
  4.  * @param request 
  5.  * @return 
  6.  */  
  7. @SuppressWarnings("unchecked")  
  8. public static Map getParameterMap(HttpServletRequest request) {  
  9.     // parameter Map  
  10.     Map properties = request.getParameterMap();  
  11.     // return value 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. }  

 

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. http://blog.csdn.net/DADADIE/article/details/52085291

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906683&siteId=291194637