mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add methods
This commit is contained in:
@@ -58,6 +58,7 @@ public class ServletUtil {
|
||||
public static final String METHOD_TRACE = "TRACE";
|
||||
|
||||
// --------------------------------------------------------- getParam start
|
||||
|
||||
/**
|
||||
* 获得所有请求参数
|
||||
*
|
||||
@@ -92,7 +93,7 @@ public class ServletUtil {
|
||||
* @since 4.0.2
|
||||
*/
|
||||
public static String getBody(ServletRequest request) {
|
||||
try(final BufferedReader reader = request.getReader()) {
|
||||
try (final BufferedReader reader = request.getReader()) {
|
||||
return IoUtil.read(reader);
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
@@ -117,12 +118,13 @@ public class ServletUtil {
|
||||
// --------------------------------------------------------- getParam end
|
||||
|
||||
// --------------------------------------------------------- fillBean start
|
||||
|
||||
/**
|
||||
* ServletRequest 参数转Bean
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param request ServletRequest
|
||||
* @param bean Bean
|
||||
* @param <T> Bean类型
|
||||
* @param request ServletRequest
|
||||
* @param bean Bean
|
||||
* @param copyOptions 注入时的设置
|
||||
* @return Bean
|
||||
* @since 3.0.4
|
||||
@@ -133,17 +135,17 @@ public class ServletUtil {
|
||||
@Override
|
||||
public Object value(String key, Type valueType) {
|
||||
String[] values = request.getParameterValues(key);
|
||||
if(ArrayUtil.isEmpty(values)){
|
||||
if (ArrayUtil.isEmpty(values)) {
|
||||
values = request.getParameterValues(beanName + StrUtil.DOT + key);
|
||||
if(ArrayUtil.isEmpty(values)){
|
||||
if (ArrayUtil.isEmpty(values)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if(1 == values.length){
|
||||
if (1 == values.length) {
|
||||
// 单值表单直接返回这个值
|
||||
return values[0];
|
||||
}else{
|
||||
} else {
|
||||
// 多值表单返回数组
|
||||
return values;
|
||||
}
|
||||
@@ -160,9 +162,9 @@ public class ServletUtil {
|
||||
/**
|
||||
* ServletRequest 参数转Bean
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param request {@link ServletRequest}
|
||||
* @param bean Bean
|
||||
* @param <T> Bean类型
|
||||
* @param request {@link ServletRequest}
|
||||
* @param bean Bean
|
||||
* @param isIgnoreError 是否忽略注入错误
|
||||
* @return Bean
|
||||
*/
|
||||
@@ -173,9 +175,9 @@ public class ServletUtil {
|
||||
/**
|
||||
* ServletRequest 参数转Bean
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param request ServletRequest
|
||||
* @param beanClass Bean Class
|
||||
* @param <T> Bean类型
|
||||
* @param request ServletRequest
|
||||
* @param beanClass Bean Class
|
||||
* @param isIgnoreError 是否忽略注入错误
|
||||
* @return Bean
|
||||
*/
|
||||
@@ -202,12 +204,12 @@ public class ServletUtil {
|
||||
* 需要注意的是,使用此方法获取的客户IP地址必须在Http服务器(例如Nginx)中配置头信息,否则容易造成IP伪造。
|
||||
* </p>
|
||||
*
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param otherHeaderNames 其他自定义头文件,通常在Http服务器(例如Nginx)中配置
|
||||
* @return IP地址
|
||||
*/
|
||||
public static String getClientIP(HttpServletRequest request, String... otherHeaderNames) {
|
||||
String[] headers = { "X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR" };
|
||||
String[] headers = {"X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};
|
||||
if (ArrayUtil.isNotEmpty(otherHeaderNames)) {
|
||||
headers = ArrayUtil.addAll(headers, otherHeaderNames);
|
||||
}
|
||||
@@ -223,7 +225,7 @@ public class ServletUtil {
|
||||
* 需要注意的是,使用此方法获取的客户IP地址必须在Http服务器(例如Nginx)中配置头信息,否则容易造成IP伪造。
|
||||
* </p>
|
||||
*
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param headerNames 自定义头,通常在Http服务器(例如Nginx)中配置
|
||||
* @return IP地址
|
||||
* @since 4.4.1
|
||||
@@ -258,7 +260,7 @@ public class ServletUtil {
|
||||
* 包括文件和普通表单数据<br>
|
||||
* 在同一次请求中,此方法只能被执行一次!
|
||||
*
|
||||
* @param request {@link ServletRequest}
|
||||
* @param request {@link ServletRequest}
|
||||
* @param uploadSetting 上传文件的设定,包括最大文件大小、保存在内存的边界大小、临时目录、扩展名限定等
|
||||
* @return MultiPart表单
|
||||
* @throws IORuntimeException IO异常
|
||||
@@ -276,6 +278,7 @@ public class ServletUtil {
|
||||
}
|
||||
|
||||
// --------------------------------------------------------- Header start
|
||||
|
||||
/**
|
||||
* 获取请求所有的头(header)信息
|
||||
*
|
||||
@@ -300,7 +303,7 @@ public class ServletUtil {
|
||||
/**
|
||||
* 忽略大小写获得请求header中的信息
|
||||
*
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param nameIgnoreCase 忽略大小写头信息的KEY
|
||||
* @return header值
|
||||
*/
|
||||
@@ -320,8 +323,8 @@ public class ServletUtil {
|
||||
/**
|
||||
* 获得请求header中的信息
|
||||
*
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param name 头信息的KEY
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param name 头信息的KEY
|
||||
* @param charsetName 字符集
|
||||
* @return header值
|
||||
*/
|
||||
@@ -333,7 +336,7 @@ public class ServletUtil {
|
||||
* 获得请求header中的信息
|
||||
*
|
||||
* @param request 请求对象{@link HttpServletRequest}
|
||||
* @param name 头信息的KEY
|
||||
* @param name 头信息的KEY
|
||||
* @param charset 字符集
|
||||
* @return header值
|
||||
* @since 4.6.2
|
||||
@@ -402,11 +405,12 @@ public class ServletUtil {
|
||||
// --------------------------------------------------------- Header end
|
||||
|
||||
// --------------------------------------------------------- Cookie start
|
||||
|
||||
/**
|
||||
* 获得指定的Cookie
|
||||
*
|
||||
* @param httpServletRequest {@link HttpServletRequest}
|
||||
* @param name cookie名
|
||||
* @param name cookie名
|
||||
* @return Cookie对象
|
||||
*/
|
||||
public static Cookie getCookie(HttpServletRequest httpServletRequest, String name) {
|
||||
@@ -421,7 +425,7 @@ public class ServletUtil {
|
||||
*/
|
||||
public static Map<String, Cookie> readCookieMap(HttpServletRequest httpServletRequest) {
|
||||
final Cookie[] cookies = httpServletRequest.getCookies();
|
||||
if(ArrayUtil.isEmpty(cookies)){
|
||||
if (ArrayUtil.isEmpty(cookies)) {
|
||||
return MapUtil.empty();
|
||||
}
|
||||
|
||||
@@ -435,7 +439,7 @@ public class ServletUtil {
|
||||
* 设定返回给客户端的Cookie
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param cookie Servlet Cookie对象
|
||||
* @param cookie Servlet Cookie对象
|
||||
*/
|
||||
public static void addCookie(HttpServletResponse response, Cookie cookie) {
|
||||
response.addCookie(cookie);
|
||||
@@ -445,8 +449,8 @@ public class ServletUtil {
|
||||
* 设定返回给客户端的Cookie
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param name Cookie名
|
||||
* @param value Cookie值
|
||||
* @param name Cookie名
|
||||
* @param value Cookie值
|
||||
*/
|
||||
public static void addCookie(HttpServletResponse response, String name, String value) {
|
||||
response.addCookie(new Cookie(name, value));
|
||||
@@ -455,12 +459,12 @@ public class ServletUtil {
|
||||
/**
|
||||
* 设定返回给客户端的Cookie
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param name cookie名
|
||||
* @param value cookie值
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param name cookie名
|
||||
* @param value cookie值
|
||||
* @param maxAgeInSeconds -1: 关闭浏览器清除Cookie. 0: 立即清除Cookie. >0 : Cookie存在的秒数.
|
||||
* @param path Cookie的有效路径
|
||||
* @param domain the domain name within which this cookie is visible; form is according to RFC 2109
|
||||
* @param path Cookie的有效路径
|
||||
* @param domain the domain name within which this cookie is visible; form is according to RFC 2109
|
||||
*/
|
||||
public static void addCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds, String path, String domain) {
|
||||
Cookie cookie = new Cookie(name, value);
|
||||
@@ -477,9 +481,9 @@ public class ServletUtil {
|
||||
* Path: "/"<br>
|
||||
* No Domain
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param name cookie名
|
||||
* @param value cookie值
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param name cookie名
|
||||
* @param value cookie值
|
||||
* @param maxAgeInSeconds -1: 关闭浏览器清除Cookie. 0: 立即清除Cookie. >0 : Cookie存在的秒数.
|
||||
*/
|
||||
public static void addCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds) {
|
||||
@@ -488,6 +492,7 @@ public class ServletUtil {
|
||||
|
||||
// --------------------------------------------------------- Cookie end
|
||||
// --------------------------------------------------------- Response start
|
||||
|
||||
/**
|
||||
* 获得PrintWriter
|
||||
*
|
||||
@@ -506,8 +511,8 @@ public class ServletUtil {
|
||||
/**
|
||||
* 返回数据给客户端
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param text 返回的内容
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param text 返回的内容
|
||||
* @param contentType 返回的类型
|
||||
*/
|
||||
public static void write(HttpServletResponse response, String text, String contentType) {
|
||||
@@ -528,7 +533,7 @@ public class ServletUtil {
|
||||
* 返回文件给客户端
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param file 写出的文件对象
|
||||
* @param file 写出的文件对象
|
||||
* @since 4.1.15
|
||||
*/
|
||||
public static void write(HttpServletResponse response, File file) {
|
||||
@@ -546,21 +551,21 @@ public class ServletUtil {
|
||||
/**
|
||||
* 返回数据给客户端
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param in 需要返回客户端的内容
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param in 需要返回客户端的内容
|
||||
* @param 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"; #原始字节流
|
||||
* @param fileName 文件名
|
||||
* 如:
|
||||
* 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"; #原始字节流
|
||||
* @param fileName 文件名
|
||||
* @since 4.1.15
|
||||
*/
|
||||
public static void write(HttpServletResponse response, InputStream in, String contentType, String fileName) {
|
||||
@@ -573,8 +578,8 @@ public class ServletUtil {
|
||||
/**
|
||||
* 返回数据给客户端
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param in 需要返回客户端的内容
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param in 需要返回客户端的内容
|
||||
* @param contentType 返回的类型
|
||||
*/
|
||||
public static void write(HttpServletResponse response, InputStream in, String contentType) {
|
||||
@@ -586,7 +591,7 @@ public class ServletUtil {
|
||||
* 返回数据给客户端
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param in 需要返回客户端的内容
|
||||
* @param in 需要返回客户端的内容
|
||||
*/
|
||||
public static void write(HttpServletResponse response, InputStream in) {
|
||||
write(response, in, IoUtil.DEFAULT_BUFFER_SIZE);
|
||||
@@ -595,8 +600,8 @@ public class ServletUtil {
|
||||
/**
|
||||
* 返回数据给客户端
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param in 需要返回客户端的内容
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param in 需要返回客户端的内容
|
||||
* @param bufferSize 缓存大小
|
||||
*/
|
||||
public static void write(HttpServletResponse response, InputStream in, int bufferSize) {
|
||||
@@ -616,15 +621,15 @@ public class ServletUtil {
|
||||
* 设置响应的Header
|
||||
*
|
||||
* @param response 响应对象{@link HttpServletResponse}
|
||||
* @param name 名
|
||||
* @param value 值,可以是String,Date, int
|
||||
* @param name 名
|
||||
* @param value 值,可以是String,Date, int
|
||||
*/
|
||||
public static void setHeader(HttpServletResponse response, String name, Object value) {
|
||||
if (value instanceof String) {
|
||||
response.setHeader(name, (String) value);
|
||||
} else if (Date.class.isAssignableFrom(value.getClass())) {
|
||||
response.setDateHeader(name, ((Date) value).getTime());
|
||||
} else if (value instanceof Integer || "int".equals(value.getClass().getSimpleName().toLowerCase())) {
|
||||
} else if (value instanceof Integer || "int".equalsIgnoreCase(value.getClass().getSimpleName())) {
|
||||
response.setIntHeader(name, (int) value);
|
||||
} else {
|
||||
response.setHeader(name, value.toString());
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.extra.servlet;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@@ -8,18 +9,22 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* ServletUtil工具类测试
|
||||
*
|
||||
* @author dazer
|
||||
* @date 2021/3/24 15:02
|
||||
* @see ServletUtil
|
||||
*/
|
||||
public class ServletUtilTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void writeTest() {
|
||||
HttpServletResponse response = null;
|
||||
byte[] bytes = new String("地球是我们共同的家园,需要大家珍惜.").getBytes(StandardCharsets.UTF_8);
|
||||
byte[] bytes = "地球是我们共同的家园,需要大家珍惜.".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
//下载文件
|
||||
// 这里没法直接测试,直接写到这里,方便调用;
|
||||
//noinspection ConstantConditions
|
||||
if (response != null) {
|
||||
String fileName = "签名文件.pdf";
|
||||
String contentType = "application/pdf";// application/octet-stream、image/jpeg、image/gif
|
||||
|
Reference in New Issue
Block a user