http工具類-獲取請求參數

<code>package com.icity.util;import com.alibaba.fastjson.JSONObject;import org.apache.commons.lang3.StringUtils;import org.springframework.http.HttpMethod;import javax.servlet.http.HttpServletRequest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.util.HashMap;import java.util.Map;import java.util.SortedMap;import java.util.TreeMap;/** * http 工具類 獲取請求中的參數 * @author wangwei * @date 2020/02/13 */public class HttpUtils { /** * 將URL的參數和body參數合併 * @author wangwei * @date 2920/02/13 * @param request */ public static SortedMap<string> getAllParams(HttpServletRequest request) throws IOException { SortedMap<string> result = new TreeMap<>(); //獲取URL上的參數 Map<string> urlParams = getUrlParams(request); if(urlParams!=null){ for (Map.Entry entry : urlParams.entrySet()) { result.put((String) entry.getKey(), (String) entry.getValue()); } }else{ urlParams=new HashMap<>(); } Map<string> allRequestParam = new HashMap<>(16); // get請求不需要拿body參數 if (!HttpMethod.GET.name().equals(request.getMethod())) { allRequestParam = getAllRequestParam(request); } //將URL的參數和body參數進行合併 if (allRequestParam != null) { for (Map.Entry entry : allRequestParam.entrySet()) { result.put((String) entry.getKey(),entry.getValue()); } } return result; } /** * 獲取 Body 參數 * @author wangwei * @date 2920/02/13 * @param request */ public static Map<string> getAllRequestParam(final HttpServletRequest request) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); String str = ""; StringBuilder wholeStr = new StringBuilder(); //一行一行的讀取body體裡面的內容; while ((str = reader.readLine()) != null) { wholeStr.append(str); } //轉化成json對象 if(StringUtils.isNotEmpty(wholeStr)){ return JSONObject.parseObject(wholeStr.toString(), Map.class); }else{ return null; } } /** * 將URL請求參數轉換成Map * @author wangwei * @date 2920/02/13 * @param request */ public static Map<string> getUrlParams(HttpServletRequest request) { String param = ""; Map<string> result = new HashMap<>(16); try { if(!StringUtils.isEmpty(request.getQueryString())){ param = URLDecoder.decode(request.getQueryString(), "utf-8"); String[] params = param.split("&"); for (String s : params) { int index = s.indexOf("="); result.put(s.substring(0, index), s.substring(index + 1)); } }else{ return null; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }}/<string>/<string>/<string>/<string>/<string>/<string>/<string>/<code>

<code>package com.icity.util;import com.alibaba.fastjson.JSONObject;import org.apache.commons.lang3.StringUtils;import org.springframework.http.HttpMethod;import javax.servlet.http.HttpServletRequest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.util.HashMap;import java.util.Map;import java.util.SortedMap;import java.util.TreeMap;/** * http 工具類 獲取請求中的參數 * @author wangwei * @date 2020/02/13 */public class HttpUtils { /** * 將URL的參數和body參數合併 * @author wangwei * @date 2920/02/13 * @param request */ public static SortedMap<string> getAllParams(HttpServletRequest request) throws IOException { SortedMap<string> result = new TreeMap<>(); //獲取URL上的參數 Map<string> urlParams = getUrlParams(request); if(urlParams!=null){ for (Map.Entry entry : urlParams.entrySet()) { result.put((String) entry.getKey(), (String) entry.getValue()); } }else{ urlParams=new HashMap<>(); } Map<string> allRequestParam = new HashMap<>(16); // get請求不需要拿body參數 if (!HttpMethod.GET.name().equals(request.getMethod())) { allRequestParam = getAllRequestParam(request); } //將URL的參數和body參數進行合併 if (allRequestParam != null) { for (Map.Entry entry : allRequestParam.entrySet()) { result.put((String) entry.getKey(),entry.getValue()); } } return result; } /** * 獲取 Body 參數 * @author wangwei * @date 2920/02/13 * @param request */ public static Map<string> getAllRequestParam(final HttpServletRequest request) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); String str = ""; StringBuilder wholeStr = new StringBuilder(); //一行一行的讀取body體裡面的內容; while ((str = reader.readLine()) != null) { wholeStr.append(str); } //轉化成json對象 if(StringUtils.isNotEmpty(wholeStr)){ return JSONObject.parseObject(wholeStr.toString(), Map.class); }else{ return null; } } /** * 將URL請求參數轉換成Map * @author wangwei * @date 2920/02/13 * @param request */ public static Map<string> getUrlParams(HttpServletRequest request) { String param = ""; Map<string> result = new HashMap<>(16); try { if(!StringUtils.isEmpty(request.getQueryString())){ param = URLDecoder.decode(request.getQueryString(), "utf-8"); String[] params = param.split("&"); for (String s : params) { int index = s.indexOf("="); result.put(s.substring(0, index), s.substring(index + 1)); } }else{ return null; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }}/<string>/<string>/<string>/<string>/<string>/<string>/<string>/<code>

更多工具類-敬請期待

<code>package com.icity.util;import com.alibaba.fastjson.JSONObject;import org.apache.commons.lang3.StringUtils;import org.springframework.http.HttpMethod;import javax.servlet.http.HttpServletRequest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.util.HashMap;import java.util.Map;import java.util.SortedMap;import java.util.TreeMap;/** * http 工具類 獲取請求中的參數 * @author wangwei * @date 2020/02/13 */public class HttpUtils { /** * 將URL的參數和body參數合併 * @author wangwei * @date 2920/02/13 * @param request */ public static SortedMap<string> getAllParams(HttpServletRequest request) throws IOException { SortedMap<string> result = new TreeMap<>(); //獲取URL上的參數 Map<string> urlParams = getUrlParams(request); if(urlParams!=null){ for (Map.Entry entry : urlParams.entrySet()) { result.put((String) entry.getKey(), (String) entry.getValue()); } }else{ urlParams=new HashMap<>(); } Map<string> allRequestParam = new HashMap<>(16); // get請求不需要拿body參數 if (!HttpMethod.GET.name().equals(request.getMethod())) { allRequestParam = getAllRequestParam(request); } //將URL的參數和body參數進行合併 if (allRequestParam != null) { for (Map.Entry entry : allRequestParam.entrySet()) { result.put((String) entry.getKey(),entry.getValue()); } } return result; } /** * 獲取 Body 參數 * @author wangwei * @date 2920/02/13 * @param request */ public static Map<string> getAllRequestParam(final HttpServletRequest request) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); String str = ""; StringBuilder wholeStr = new StringBuilder(); //一行一行的讀取body體裡面的內容; while ((str = reader.readLine()) != null) { wholeStr.append(str); } //轉化成json對象 if(StringUtils.isNotEmpty(wholeStr)){ return JSONObject.parseObject(wholeStr.toString(), Map.class); }else{ return null; } } /** * 將URL請求參數轉換成Map * @author wangwei * @date 2920/02/13 * @param request */ public static Map<string> getUrlParams(HttpServletRequest request) { String param = ""; Map<string> result = new HashMap<>(16); try { if(!StringUtils.isEmpty(request.getQueryString())){ param = URLDecoder.decode(request.getQueryString(), "utf-8"); String[] params = param.split("&"); for (String s : params) { int index = s.indexOf("="); result.put(s.substring(0, index), s.substring(index + 1)); } }else{ return null; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }}/<string>/<string>/<string>/<string>/<string>/<string>/<string>/<code>