Java獲取請求頭、參數、路徑方式
Java獲取請求頭、參數、路徑
request.getReader()和request.getParameter("key") 方法只能讀取一次,重復讀取會報IO異常
第一種
從 ContainerRequestContext 對象 獲取請求頭、路徑、參數
//請求頭 MultivaluedMap<String, String> headers = containerRequestContext.getHeaders(); //路徑參數 MultivaluedMap<String, String> pathParam = containerRequestContext.getUriInfo().getPathParameters(); //queryParam MultivaluedMap<String, String> queryParam = containerRequestContext.getUriInfo().getQueryParameters(); // 路徑 String path = containerRequestContext.getUriInfo().getPath(true).toLowerCase();
注意:
這里MultivaluedMap和map不同,遍歷的時候也不同,MultivaluedMap 一個key 可以有多個值 , map一個key 只對應一個值
舉個例子:
// MultiValueMap 一個 key 可以對應多個 value
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("name", "小明");
map.add("name", "小紅");
System.out.println(map.toString());
// Map 一個 key 對應一個 value
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", "小明");
hashMap.put("name", "小紅");
System.out.println(hashMap.toString());
--------------output---------------
{name=[小明, 小紅]}
{name=小紅}
第二種
從 HttpServletRequest 獲取
// 獲取所有header
Map<String, String> headerMap = new HashMap<>();
Enumeration enumeration = httpServletRequest.getHeaderNames();
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement();
String value = httpServletRequest.getHeader(name);
headerMap.put(name, value);
}
// 獲取所有參數
Map<String, String> parameterMap = new HashMap<>();
Enumeration enumeration = httpServletRequest.getParameterNames();
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement();
String value = httpServletRequest.getParameter(name);
parameterMap.put(name, value);
}// 獲取boby
InputStream inputStream = null;
try {
inputStream = httpServletRequest.getInputStream();
StringBuilder babyStr = new StringBuilder();
byte[] b = new byte[4096];
for (int n; (n = inputStream.read(b)) != -1; ) {
babyStr.append(new String(b, 0, n));
}
System.out.println(babyStr);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // 二進制讀取 獲取boby
int len = httpServletRequest.getContentLength();
byte[] buffer = new byte[len];
ServletInputStream in = null;
try {
in = httpServletRequest.getInputStream();
in.read(buffer, 0, len);
in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}注意:
HttpServletRequest 請求中的 body 內容僅能調用 request.getInputStream(), request.getReader()和request.getParameter("key") 方法讀取一次,重復讀取會報 java.io.IOException: Stream closed 異常
獲取路徑:
request.getServletPath() request.getPathInfo() request.getContextPath() request.getRequestURI() request.getRequestURL() request.getServletContext().getRealPath()
getServletPath():獲取能夠與“url-pattern”中匹配的路徑,注意是完全匹配的部分,*的部分不包括。getPageInfo():與getServletPath()獲取的路徑互補,能夠得到的是“url-pattern”中 模糊匹配(不確定) 的路徑部分getContextPath():獲取項目的根路徑getRequestURI():獲取根路徑到地址結尾getRequestURL():獲取請求的地址鏈接(瀏覽器中輸入的地址)getServletContext().getRealPath(“/”):獲取“/”在機器中的實際地址getScheme():獲取的是使用的協議(http 或https)getProtocol():獲取的是協議的名稱(HTTP/1.11)getServerName():獲取的是域名(xxx.com)getLocalName():獲取到的是IP
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java使用Apache POI和EasyExcel讀取Excel文件的實現方案
Java 讀取 Excel 文件核心依賴 Apache POI(兼容 .xls(Excel 97-2003)和 .xlsx(Excel 2007+))或 EasyExcel(阿里開源,低內存、高性能),以下是兩種主流方案的完整實現,需要的朋友可以參考下2025-12-12
簡單捋捋@RequestParam 和 @RequestBody的使用
這篇文章主要介紹了簡單捋捋@RequestParam 和 @RequestBody的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
SpringBoot設置HTTP響應狀態(tài)碼過程(HTTP Status Code)
本文介紹了HTTP響應狀態(tài)碼的分類及其在SpringBoot中的使用示例,狀態(tài)碼包括信息、成功、重定向、客戶端錯誤和服務器錯誤,常用的幾種狀態(tài)碼有200(請求成功)、400(客戶端錯誤)、404(資源未找到)和500(服務器內部錯誤)2026-01-01
詳解SpringBoot中RestTemplate的幾種實現
這篇文章主要介紹了詳解SpringBoot中RestTemplate的幾種實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Spring加載屬性文件方式(自動加載優(yōu)先級問題)
這篇文章主要介紹了Spring加載屬性文件方式(自動加載優(yōu)先級問題),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

