public class ServletUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
ACCESS_TOKEN |
static String |
BEARER_TYPE |
static String |
HTTP_TCP_NAME |
static String |
HTTPS_TCP_NAME |
static String |
METHOD_DELETE |
static String |
METHOD_GET |
static String |
METHOD_HEAD |
static String |
METHOD_OPTIONS |
static String |
METHOD_POST |
static String |
METHOD_PUT |
static String |
METHOD_TRACE |
static String |
POINTCUT
HttpAspect请求切入点
|
| 构造器和说明 |
|---|
ServletUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
addCookie(javax.servlet.http.Cookie cookie)
设定返回给客户端的Cookie
|
static void |
addCookie(String name,
String value)
设定返回给客户端的Cookie
|
static void |
addCookie(String name,
String value,
int maxAgeInSeconds)
设定返回给客户端的Cookie
Path: "/" No Domain |
static void |
addCookie(String name,
String value,
int maxAgeInSeconds,
String path,
String domain)
设定返回给客户端的Cookie
|
static <T> T |
fillBean(javax.servlet.ServletRequest request,
T bean,
boolean isIgnoreError)
ServletRequest 参数转Bean
|
static <T> T |
fillBean(javax.servlet.ServletRequest request,
T bean,
cn.hutool.core.bean.copier.CopyOptions copyOptions)
ServletRequest 参数转Bean
|
static String |
getAuthToken()
获得请求中的OAuth2 Token
|
static String |
getAuthToken(javax.servlet.http.HttpServletRequest request)
获得请求中的OAuth2 Token
|
static String |
getBody(javax.servlet.ServletRequest request)
获取请求体
调用该方法后,getParam方法将失效 |
static byte[] |
getBodyBytes(javax.servlet.ServletRequest request)
获取请求体byte[]
调用该方法后,getParam方法将失效 |
static String |
getClientIP(String... otherHeaderNames)
获取客户端IP
默认检测的Header:
1、X-Forwarded-For
2、X-Real-IP
3、Proxy-Client-IP
4、WL-Proxy-Client-IP
otherHeaderNames参数用于自定义检测的Header
需要注意的是,使用此方法获取的客户IP地址必须在Http服务器(例如Nginx)中配置头信息,否则容易造成IP伪造。 |
static String |
getClientIPByHeader(String... headerNames)
获取客户端IP
headerNames参数用于自定义检测的Header
需要注意的是,使用此方法获取的客户IP地址必须在Http服务器(例如Nginx)中配置头信息,否则容易造成IP伪造。 |
static javax.servlet.http.Cookie |
getCookie(String name)
获得指定的Cookie
|
static String |
getHeader(String name,
Charset charset)
获得请求header中的信息
|
static String |
getHeader(String name,
String charsetName)
获得请求header中的信息
|
static String |
getHeaderIgnoreCase(String nameIgnoreCase)
忽略大小写获得请求header中的信息
|
static Map<String,String> |
getHeaderMap()
获取请求所有的头(header)信息
|
static MultipartFormData |
getMultipart()
获得multipart/form-data 表单内容
包括文件和普通表单数据 在同一次请求中,此方法只能被执行一次! |
static Map<String,String> |
getParamMap(javax.servlet.ServletRequest request)
获得所有请求参数
|
static Map<String,String[]> |
getParams(javax.servlet.ServletRequest request)
获得所有请求参数
|
static javax.servlet.http.HttpServletRequest |
getRequest()
获得当前请求上下文中的HttpServletRequest
|
static org.springframework.web.context.request.ServletRequestAttributes |
getRequestAttributes()
获得当前请求上下文中的ServletRequestAttributes
|
static javax.servlet.http.HttpServletResponse |
getResponse()
获得当前请求上下文中的HttpServletResponse
|
static String |
getServerURL()
获得当前请求的服务器的URL地址
示例一:http://localhost:8080
示例二:http://localhost:8080/projectName |
static javax.servlet.http.HttpSession |
getSession()
获得当前请求HttpSession
|
static PrintWriter |
getWriter()
获得PrintWriter
|
static boolean |
isGetMethod()
是否为GET请求
|
static boolean |
isIE()
客户浏览器是否为IE
|
static boolean |
isMultipart()
是否为Multipart类型表单,此类型表单用于文件上传
|
static boolean |
isPostMethod()
是否为POST请求
|
static void |
printRequest()
打印请求报文
注意:打印不包括:异步请求内容、数据流
|
static Map<String,javax.servlet.http.Cookie> |
readCookieMap()
将cookie封装到Map里面
|
static void |
setHeader(String name,
Object value)
设置响应的Header
|
static <T> T |
toBean(javax.servlet.ServletRequest request,
Class<T> beanClass,
boolean isIgnoreError)
ServletRequest 参数转Bean
|
static void |
write(File file)
返回文件给客户端
|
static void |
write(InputStream in)
返回数据给客户端
|
static void |
write(InputStream in,
int bufferSize)
返回数据给客户端
|
static void |
write(InputStream in,
String contentType)
返回数据给客户端
|
static void |
write(InputStream in,
String contentType,
String fileName)
返回数据给客户端
|
static void |
write(String text,
String contentType)
返回数据给客户端
|
public static org.springframework.web.context.request.ServletRequestAttributes getRequestAttributes()
public static javax.servlet.http.HttpServletRequest getRequest()
public static javax.servlet.http.HttpServletResponse getResponse()
public static javax.servlet.http.HttpSession getSession()
public static String getAuthToken()
public static String getAuthToken(javax.servlet.http.HttpServletRequest request)
request - 请求对象ServletRequestpublic static String getServerURL()
示例一:http://localhost:8080
示例二:http://localhost:8080/projectName
public static void printRequest()
注意:打印不包括:异步请求内容、数据流
public static Map<String,String[]> getParams(javax.servlet.ServletRequest request)
request - 请求对象ServletRequestpublic static Map<String,String> getParamMap(javax.servlet.ServletRequest request)
request - 请求对象ServletRequestpublic static String getBody(javax.servlet.ServletRequest request)
request - ServletRequestpublic static byte[] getBodyBytes(javax.servlet.ServletRequest request)
request - ServletRequestpublic static <T> T fillBean(javax.servlet.ServletRequest request,
T bean,
cn.hutool.core.bean.copier.CopyOptions copyOptions)
T - Bean类型request - ServletRequestbean - BeancopyOptions - 注入时的设置public static <T> T fillBean(javax.servlet.ServletRequest request,
T bean,
boolean isIgnoreError)
T - Bean类型request - ServletRequestbean - BeanisIgnoreError - 是否忽略注入错误public static <T> T toBean(javax.servlet.ServletRequest request,
Class<T> beanClass,
boolean isIgnoreError)
T - Bean类型request - ServletRequestbeanClass - Bean ClassisIgnoreError - 是否忽略注入错误public static String getClientIP(String... otherHeaderNames)
默认检测的Header:
1、X-Forwarded-For 2、X-Real-IP 3、Proxy-Client-IP 4、WL-Proxy-Client-IP
otherHeaderNames参数用于自定义检测的Header
需要注意的是,使用此方法获取的客户IP地址必须在Http服务器(例如Nginx)中配置头信息,否则容易造成IP伪造。
otherHeaderNames - 其他自定义头文件,通常在Http服务器(例如Nginx)中配置public static String getClientIPByHeader(String... headerNames)
headerNames参数用于自定义检测的Header
需要注意的是,使用此方法获取的客户IP地址必须在Http服务器(例如Nginx)中配置头信息,否则容易造成IP伪造。
headerNames - 自定义头,通常在Http服务器(例如Nginx)中配置public static MultipartFormData getMultipart() throws cn.hutool.core.io.IORuntimeException
cn.hutool.core.io.IORuntimeException - IO异常public static Map<String,String> getHeaderMap()
public static String getHeaderIgnoreCase(String nameIgnoreCase)
nameIgnoreCase - 忽略大小写头信息的KEYpublic static String getHeader(String name, String charsetName)
name - 头信息的KEYcharsetName - 字符集public static String getHeader(String name, Charset charset)
name - 头信息的KEYcharset - 字符集public static boolean isIE()
public static boolean isGetMethod()
public static boolean isPostMethod()
public static boolean isMultipart()
public static javax.servlet.http.Cookie getCookie(String name)
name - cookie名public static Map<String,javax.servlet.http.Cookie> readCookieMap()
public static void addCookie(javax.servlet.http.Cookie cookie)
cookie - Servlet Cookie对象public static void addCookie(String name, String value)
name - Cookie名value - Cookie值public static void addCookie(String name, String value, int maxAgeInSeconds, String path, String domain)
name - cookie名value - cookie值maxAgeInSeconds - -1: 关闭浏览器清除Cookie. 0: 立即清除Cookie. >0 : Cookie存在的秒数.path - Cookie的有效路径domain - the domain name within which this cookie is visible; form is according to RFC 2109public static void addCookie(String name, String value, int maxAgeInSeconds)
name - cookie名value - cookie值maxAgeInSeconds - -1: 关闭浏览器清除Cookie. 0: 立即清除Cookie. >0 : Cookie存在的秒数.public static PrintWriter getWriter() throws cn.hutool.core.io.IORuntimeException
cn.hutool.core.io.IORuntimeException - IO异常public static void write(String text, String contentType)
text - 返回的内容contentType - 返回的类型public static void write(File file)
file - 写出的文件对象public static void write(InputStream in, String contentType, String fileName)
in - 需要返回客户端的内容contentType - 返回的类型fileName - 文件名public static void write(InputStream in, String contentType)
in - 需要返回客户端的内容contentType - 返回的类型public static void write(InputStream in)
in - 需要返回客户端的内容public static void write(InputStream in, int bufferSize)
in - 需要返回客户端的内容bufferSize - 缓存大小Copyright © 2021 Pivotal Software, Inc.. All rights reserved.