public class ParameterUtils {
public static Map<String, String>
getRequestParameters(
HttpServletRequest request) {
Map<String, String> map =
new HashMap<String, String>();
Map<String, String[]> paramMap = request.getParameterMap();
for (Iterator<String> it = paramMap.keySet().iterator(); it.hasNext();) {
String key = it.next();
map.put(key, paramMap.get(key)[
0]);
}
return map;
}
public static String
getRequestQueryString(Map<String, String> params) {
Map<String, String> resultMap = sortMapByKey(params);
StringBuffer buffer =
new StringBuffer();
for (Iterator<String> it = resultMap.keySet().iterator(); it.hasNext();) {
String key = it.next();
if(StringUtils.equalsIgnoreCase(
"callback", key)){
continue;
}
buffer.append(key +
"=" + resultMap.get(key) +
"&");
}
return buffer.substring(
0, buffer.length()-
1);
}
/**
* 使用 Map按key进行排序
* @param map
* @return
*/
public static Map<String, String>
sortMapByKey(Map<String, String> map) {
if (map ==
null || map.isEmpty()) {
return null;
}
Map<String, String> sortMap =
new TreeMap<String, String>(
new MapKeyComparator());
sortMap.putAll(map);
return sortMap;
}
}
MapKeyComparator
public class MapKeyComparator implements Comparator<String>{
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
}