public class ServletUtils extends Object
Modifier and Type | Field and Description |
---|---|
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请求切入点
|
Constructor and Description |
---|
ServletUtils() |
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,
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(javax.servlet.http.HttpServletRequest request,
String... otherHeaderNames)
获取客户端IP
|
static String |
getClientIP(String... otherHeaderNames)
获取客户端IP
|
static String |
getClientIPByHeader(javax.servlet.http.HttpServletRequest request,
String... headerNames)
获取客户端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 cn.hutool.core.net.multipart.MultipartFormData |
getMultipart(javax.servlet.ServletRequest request)
获得MultiPart表单内容,多用于获得上传的文件 在同一次请求中,此方法只能被执行一次!
|
static cn.hutool.core.net.multipart.MultipartFormData |
getMultipart(javax.servlet.ServletRequest request,
cn.hutool.core.net.multipart.UploadSetting uploadSetting)
获得multipart/form-data 表单内容
包括文件和普通表单数据 在同一次请求中,此方法只能被执行一次! |
static Map<String,String> |
getParamMap(javax.servlet.ServletRequest request)
获得所有请求参数
|
static Map<String,String[]> |
getParams(javax.servlet.ServletRequest request)
获得所有请求参数
|
static <T> T |
getParamToJavaBean(Class<T> clazz)
获得请求参数并转换为JavaBean
|
static <T> T |
getParamToJavaBean(javax.servlet.http.HttpServletRequest request,
Class<T> clazz)
获得请求参数并转换为JavaBean
|
static com.alibaba.fastjson.JSONObject |
getParamToJson()
获得请求Json
|
static com.alibaba.fastjson.JSONObject |
getParamToJson(javax.servlet.http.HttpServletRequest request)
获得请求Json
|
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地址
|
static javax.servlet.http.HttpSession |
getSession()
获得当前请求HttpSession
|
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 void |
printRequest()
打印请求报文
|
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 final String HTTP_TCP_NAME
public static final String HTTPS_TCP_NAME
public static final String BEARER_TYPE
public static final String ACCESS_TOKEN
public static final String POINTCUT
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
- 请求对象ServletRequest
public static com.alibaba.fastjson.JSONObject getParamToJson()
public static com.alibaba.fastjson.JSONObject getParamToJson(javax.servlet.http.HttpServletRequest request)
request
- 请求对象ServletRequest
public static <T> T getParamToJavaBean(Class<T> clazz)
public static <T> T getParamToJavaBean(javax.servlet.http.HttpServletRequest request, Class<T> clazz)
request
- 请求对象ServletRequest
public 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
- 请求对象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, 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
- 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(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 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 cn.hutool.core.net.multipart.MultipartFormData getMultipart(javax.servlet.ServletRequest request) throws cn.hutool.core.io.IORuntimeException
request
- ServletRequest
cn.hutool.core.io.IORuntimeException
- IO异常public static cn.hutool.core.net.multipart.MultipartFormData getMultipart(javax.servlet.ServletRequest request, cn.hutool.core.net.multipart.UploadSetting uploadSetting) throws cn.hutool.core.io.IORuntimeException
request
- ServletRequest
uploadSetting
- 上传文件的设定,包括最大文件大小、保存在内存的边界大小、临时目录、扩展名限定等cn.hutool.core.io.IORuntimeException
- IO异常public static Map<String,String> getHeaderMap(javax.servlet.http.HttpServletRequest request)
request
- 请求对象HttpServletRequest
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 cn.hutool.core.io.IORuntimeException
response
- 响应对象HttpServletResponse
cn.hutool.core.io.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
- 返回的类型
如:
1、application/pdf、
2、application/vnd.ms-excel、
3、application/msword、
4、application/vnd.ms-powerpoint
docx、xlsx 这种 office 2007 格式 设置 MIME;网页里面docx 文件是没问题,但是下载下来了之后就变成doc格式了
https://blog.csdn.net/cyh2260629/article/details/73824760
5、MIME_EXCELX_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
6、MIME_PPTX_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
7、MIME_WORDX_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
8、MIME_STREAM_TYPE = "application/octet-stream;charset=utf-8"; #原始字节流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 © 2022 Pivotal Software, Inc.. All rights reserved.