public class ServletUtil extends Object
Modifier and Type | Field and Description |
---|---|
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 |
Constructor and Description |
---|
ServletUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
addCookie(javax.servlet.http.HttpServletResponse response,
javax.servlet.http.Cookie cookie)
设定返回给客户端的Cookie
|
static void |
addCookie(javax.servlet.http.HttpServletResponse response,
String name,
String value)
设定返回给客户端的Cookie
|
static void |
addCookie(javax.servlet.http.HttpServletResponse response,
String name,
String value,
int maxAgeInSeconds)
设定返回给客户端的Cookie
Path: "/" No Domain |
static void |
addCookie(javax.servlet.http.HttpServletResponse response,
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,
CopyOptions copyOptions)
ServletRequest 参数转Bean
|
static String |
getBody(javax.servlet.ServletRequest request)
获取请求体
调用该方法后,getParam方法将失效 |
static byte[] |
getBodyBytes(javax.servlet.ServletRequest request)
获取请求体byte[]
调用该方法后,getParam方法将失效 |
static String |
getClientIP(javax.servlet.http.HttpServletRequest request,
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(javax.servlet.http.HttpServletRequest request,
String... headerNames)
获取客户端IP
headerNames参数用于自定义检测的Header
需要注意的是,使用此方法获取的客户IP地址必须在Http服务器(例如Nginx)中配置头信息,否则容易造成IP伪造。 |
static javax.servlet.http.Cookie |
getCookie(javax.servlet.http.HttpServletRequest httpServletRequest,
String name)
获得指定的Cookie
|
static String |
getHeader(javax.servlet.http.HttpServletRequest request,
String name,
Charset charset)
获得请求header中的信息
|
static String |
getHeader(javax.servlet.http.HttpServletRequest request,
String name,
String charsetName)
获得请求header中的信息
|
static String |
getHeaderIgnoreCase(javax.servlet.http.HttpServletRequest request,
String nameIgnoreCase)
忽略大小写获得请求header中的信息
|
static Map<String,String> |
getHeaderMap(javax.servlet.http.HttpServletRequest request)
获取请求所有的头(header)信息
|
static Map<String,List<String>> |
getHeadersMap(javax.servlet.http.HttpServletRequest request)
获取请求所有的头(header)信息
|
static Map<String,Collection<String>> |
getHeadersMap(javax.servlet.http.HttpServletResponse response)
获取响应所有的头(header)信息
|
static MultipartFormData |
getMultipart(javax.servlet.ServletRequest request)
获得MultiPart表单内容,多用于获得上传的文件 在同一次请求中,此方法只能被执行一次!
|
static MultipartFormData |
getMultipart(javax.servlet.ServletRequest request,
UploadSetting uploadSetting)
获得multipart/form-data 表单内容
包括文件和普通表单数据 在同一次请求中,此方法只能被执行一次! |
static Map<String,String> |
getParamMap(javax.servlet.ServletRequest request)
获得所有请求参数
|
static Map<String,String[]> |
getParams(javax.servlet.ServletRequest request)
获得所有请求参数
|
static PrintWriter |
getWriter(javax.servlet.http.HttpServletResponse response)
获得PrintWriter
|
static boolean |
isGetMethod(javax.servlet.http.HttpServletRequest request)
是否为GET请求
|
static boolean |
isIE(javax.servlet.http.HttpServletRequest request)
客户浏览器是否为IE
|
static boolean |
isMultipart(javax.servlet.http.HttpServletRequest request)
是否为Multipart类型表单,此类型表单用于文件上传
|
static boolean |
isPostMethod(javax.servlet.http.HttpServletRequest request)
是否为POST请求
|
static Map<String,javax.servlet.http.Cookie> |
readCookieMap(javax.servlet.http.HttpServletRequest httpServletRequest)
将cookie封装到Map里面
|
static void |
setHeader(javax.servlet.http.HttpServletResponse response,
String name,
Object value)
设置响应的Header
|
static <T> T |
toBean(javax.servlet.ServletRequest request,
Class<T> beanClass,
boolean isIgnoreError)
ServletRequest 参数转Bean
|
static void |
write(javax.servlet.http.HttpServletResponse response,
File file)
返回文件给客户端
|
static void |
write(javax.servlet.http.HttpServletResponse response,
InputStream in)
返回数据给客户端
|
static void |
write(javax.servlet.http.HttpServletResponse response,
InputStream in,
int bufferSize)
返回数据给客户端
|
static void |
write(javax.servlet.http.HttpServletResponse response,
InputStream in,
String contentType)
返回数据给客户端
|
static void |
write(javax.servlet.http.HttpServletResponse response,
InputStream in,
String contentType,
String fileName)
返回数据给客户端
|
static void |
write(javax.servlet.http.HttpServletResponse response,
String text,
String contentType)
返回数据给客户端
|
public static final String METHOD_DELETE
public static final String METHOD_HEAD
public static final String METHOD_GET
public static final String METHOD_OPTIONS
public static final String METHOD_POST
public static final String METHOD_PUT
public static final String METHOD_TRACE
public static Map<String,String[]> getParams(javax.servlet.ServletRequest request)
request
- 请求对象ServletRequest
public static Map<String,String> getParamMap(javax.servlet.ServletRequest request)
request
- 请求对象ServletRequest
public static String getBody(javax.servlet.ServletRequest request)
request
- ServletRequest
public static byte[] getBodyBytes(javax.servlet.ServletRequest request)
request
- ServletRequest
public static <T> T fillBean(javax.servlet.ServletRequest request, T bean, CopyOptions copyOptions)
T
- Bean类型request
- ServletRequestbean
- BeancopyOptions
- 注入时的设置public static <T> T fillBean(javax.servlet.ServletRequest request, T bean, boolean isIgnoreError)
T
- Bean类型request
- ServletRequest
bean
- 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(javax.servlet.http.HttpServletRequest request, 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伪造。
request
- 请求对象HttpServletRequest
otherHeaderNames
- 其他自定义头文件,通常在Http服务器(例如Nginx)中配置public static String getClientIPByHeader(javax.servlet.http.HttpServletRequest request, String... headerNames)
headerNames参数用于自定义检测的Header
需要注意的是,使用此方法获取的客户IP地址必须在Http服务器(例如Nginx)中配置头信息,否则容易造成IP伪造。
request
- 请求对象HttpServletRequest
headerNames
- 自定义头,通常在Http服务器(例如Nginx)中配置public static MultipartFormData getMultipart(javax.servlet.ServletRequest request) throws IORuntimeException
request
- ServletRequest
IORuntimeException
- IO异常public static MultipartFormData getMultipart(javax.servlet.ServletRequest request, UploadSetting uploadSetting) throws IORuntimeException
request
- ServletRequest
uploadSetting
- 上传文件的设定,包括最大文件大小、保存在内存的边界大小、临时目录、扩展名限定等IORuntimeException
- IO异常public static Map<String,String> getHeaderMap(javax.servlet.http.HttpServletRequest request)
request
- 请求对象HttpServletRequest
public static Map<String,List<String>> getHeadersMap(javax.servlet.http.HttpServletRequest request)
request
- 请求对象HttpServletRequest
public static Map<String,Collection<String>> getHeadersMap(javax.servlet.http.HttpServletResponse response)
response
- 响应对象HttpServletResponse
public static String getHeaderIgnoreCase(javax.servlet.http.HttpServletRequest request, String nameIgnoreCase)
request
- 请求对象HttpServletRequest
nameIgnoreCase
- 忽略大小写头信息的KEYpublic static String getHeader(javax.servlet.http.HttpServletRequest request, String name, String charsetName)
request
- 请求对象HttpServletRequest
name
- 头信息的KEYcharsetName
- 字符集public static String getHeader(javax.servlet.http.HttpServletRequest request, String name, Charset charset)
request
- 请求对象HttpServletRequest
name
- 头信息的KEYcharset
- 字符集public static boolean isIE(javax.servlet.http.HttpServletRequest request)
request
- 请求对象HttpServletRequest
public static boolean isGetMethod(javax.servlet.http.HttpServletRequest request)
request
- 请求对象HttpServletRequest
public static boolean isPostMethod(javax.servlet.http.HttpServletRequest request)
request
- 请求对象HttpServletRequest
public static boolean isMultipart(javax.servlet.http.HttpServletRequest request)
request
- 请求对象HttpServletRequest
public static javax.servlet.http.Cookie getCookie(javax.servlet.http.HttpServletRequest httpServletRequest, String name)
httpServletRequest
- HttpServletRequest
name
- cookie名public static Map<String,javax.servlet.http.Cookie> readCookieMap(javax.servlet.http.HttpServletRequest httpServletRequest)
httpServletRequest
- HttpServletRequest
public static void addCookie(javax.servlet.http.HttpServletResponse response, javax.servlet.http.Cookie cookie)
response
- 响应对象HttpServletResponse
cookie
- Servlet Cookie对象public static void addCookie(javax.servlet.http.HttpServletResponse response, String name, String value)
response
- 响应对象HttpServletResponse
name
- Cookie名value
- Cookie值public static void addCookie(javax.servlet.http.HttpServletResponse response, String name, String value, int maxAgeInSeconds, String path, String domain)
response
- 响应对象HttpServletResponse
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(javax.servlet.http.HttpServletResponse response, String name, String value, int maxAgeInSeconds)
response
- 响应对象HttpServletResponse
name
- cookie名value
- cookie值maxAgeInSeconds
- -1: 关闭浏览器清除Cookie. 0: 立即清除Cookie. >0 : Cookie存在的秒数.public static PrintWriter getWriter(javax.servlet.http.HttpServletResponse response) throws IORuntimeException
response
- 响应对象HttpServletResponse
IORuntimeException
- IO异常public static void write(javax.servlet.http.HttpServletResponse response, String text, String contentType)
response
- 响应对象HttpServletResponse
text
- 返回的内容contentType
- 返回的类型public static void write(javax.servlet.http.HttpServletResponse response, File file)
response
- 响应对象HttpServletResponse
file
- 写出的文件对象public static void write(javax.servlet.http.HttpServletResponse response, InputStream in, String contentType, String fileName)
response
- 响应对象HttpServletResponse
in
- 需要返回客户端的内容contentType
- 返回的类型,可以使用FileUtil.getMimeType(String)
获取对应扩展名的MIME信息
fileName
- 文件名,自动添加双引号public static void write(javax.servlet.http.HttpServletResponse response, InputStream in, String contentType)
response
- 响应对象HttpServletResponse
in
- 需要返回客户端的内容contentType
- 返回的类型public static void write(javax.servlet.http.HttpServletResponse response, InputStream in)
response
- 响应对象HttpServletResponse
in
- 需要返回客户端的内容public static void write(javax.servlet.http.HttpServletResponse response, InputStream in, int bufferSize)
response
- 响应对象HttpServletResponse
in
- 需要返回客户端的内容bufferSize
- 缓存大小Copyright © 2024. All rights reserved.