提交 | 用户 | age
|
f3ad4b
|
1 |
package com.hx.util; |
C |
2 |
|
|
3 |
import javax.servlet.http.HttpServletRequest; |
|
4 |
import java.io.BufferedReader; |
|
5 |
import java.io.IOException; |
|
6 |
import java.util.Enumeration; |
|
7 |
import java.util.HashMap; |
|
8 |
import java.util.Map; |
|
9 |
|
|
10 |
/** HttpServletRequestUtil 获取请求中的body |
|
11 |
* @author wangrenhuang |
|
12 |
* @Date 2021-10-19 |
|
13 |
*/ |
|
14 |
public class HttpServletRequestUtil { |
|
15 |
|
|
16 |
/* |
|
17 |
* 获取bady |
|
18 |
* */ |
|
19 |
public static String getBody(HttpServletRequest request) { |
|
20 |
BufferedReader br = null; |
|
21 |
StringBuilder sb = new StringBuilder(""); |
|
22 |
String str ; |
|
23 |
try { |
|
24 |
br = request.getReader(); |
|
25 |
while((str = br.readLine()) != null){ |
|
26 |
sb.append(str); |
|
27 |
} |
|
28 |
} catch (IOException e) { |
|
29 |
e.printStackTrace(); |
|
30 |
}finally { |
|
31 |
try { |
|
32 |
if (null != br){ |
|
33 |
br.close(); |
|
34 |
} |
|
35 |
} catch (IOException e) { |
|
36 |
e.printStackTrace(); |
|
37 |
} |
|
38 |
} |
|
39 |
return sb.toString(); |
|
40 |
} |
|
41 |
|
|
42 |
|
|
43 |
/* |
|
44 |
* |
|
45 |
* 获取请求头 |
|
46 |
* */ |
|
47 |
public static Map<String,String> getHeader(HttpServletRequest request) { |
|
48 |
Enumeration<String> enumeration = request.getHeaderNames(); |
|
49 |
Map<String,String> map = new HashMap<>(16); |
|
50 |
StringBuffer headers = new StringBuffer(); |
|
51 |
while (enumeration.hasMoreElements()) { |
|
52 |
String name = enumeration.nextElement(); |
|
53 |
String value = request.getHeader(name); |
|
54 |
map.put(name,value); |
|
55 |
} |
|
56 |
|
|
57 |
return map; |
|
58 |
} |
|
59 |
} |