Java獲取請(qǐng)求頭、參數(shù)、路徑方式
Java獲取請(qǐng)求頭、參數(shù)、路徑
request.getReader()和request.getParameter("key") 方法只能讀取一次,重復(fù)讀取會(huì)報(bào)IO異常
第一種
從 ContainerRequestContext 對(duì)象 獲取請(qǐng)求頭、路徑、參數(shù)
//請(qǐng)求頭 MultivaluedMap<String, String> headers = containerRequestContext.getHeaders(); //路徑參數(shù) MultivaluedMap<String, String> pathParam = containerRequestContext.getUriInfo().getPathParameters(); //queryParam MultivaluedMap<String, String> queryParam = containerRequestContext.getUriInfo().getQueryParameters(); // 路徑 String path = containerRequestContext.getUriInfo().getPath(true).toLowerCase();
注意:
這里MultivaluedMap和map不同,遍歷的時(shí)候也不同,MultivaluedMap 一個(gè)key 可以有多個(gè)值 , map一個(gè)key 只對(duì)應(yīng)一個(gè)值
舉個(gè)例子:
// MultiValueMap 一個(gè) key 可以對(duì)應(yīng)多個(gè) value MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.add("name", "小明"); map.add("name", "小紅"); System.out.println(map.toString()); // Map 一個(gè) key 對(duì)應(yīng)一個(gè) 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); }
// 獲取所有參數(shù) 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(); } } }
// 二進(jìn)制讀取 獲取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 請(qǐng)求中的 body 內(nèi)容僅能調(diào)用 request.getInputStream(), request.getReader()和request.getParameter("key") 方法讀取一次,重復(fù)讀取會(huì)報(bào) java.io.IOException: Stream closed 異常
獲取路徑:
request.getServletPath() request.getPathInfo() request.getContextPath() request.getRequestURI() request.getRequestURL() request.getServletContext().getRealPath()
getServletPath()
:獲取能夠與“url-pattern”中匹配的路徑,注意是完全匹配的部分,*的部分不包括。getPageInfo()
:與getServletPath()獲取的路徑互補(bǔ),能夠得到的是“url-pattern”中 模糊匹配(不確定) 的路徑部分getContextPath()
:獲取項(xiàng)目的根路徑getRequestURI()
:獲取根路徑到地址結(jié)尾getRequestURL()
:獲取請(qǐng)求的地址鏈接(瀏覽器中輸入的地址)getServletContext().getRealPath(“/”)
:獲取“/”在機(jī)器中的實(shí)際地址getScheme()
:獲取的是使用的協(xié)議(http 或https)getProtocol()
:獲取的是協(xié)議的名稱(HTTP/1.11)getServerName()
:獲取的是域名(xxx.com)getLocalName()
:獲取到的是IP
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring+Jpa多數(shù)據(jù)源配置的方法示例
這篇文章主要介紹了spring+Jpa多數(shù)據(jù)源配置的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Java編程實(shí)現(xiàn)beta分布的采樣或抽樣實(shí)例代碼
這篇文章主要介紹了Java編程實(shí)現(xiàn)beta分布的采樣或抽樣實(shí)例,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Java中的BlockingQueue阻塞隊(duì)列原理以及實(shí)現(xiàn)詳解
這篇文章主要介紹了Java中的BlockingQueue阻塞隊(duì)列原理以及實(shí)現(xiàn)詳解,在最常見(jiàn)的使用到這個(gè)阻塞隊(duì)列的地方,就是我們耳熟能詳?shù)木€程池里面了,作為我們線程池的一大最大參與者,也是AQS的一個(gè)具體實(shí)現(xiàn),需要的朋友可以參考下2023-12-12