package org.dromara.common.core.utils;
|
|
import cn.hutool.core.convert.Convert;
|
import cn.hutool.extra.servlet.JakartaServletUtil;
|
import cn.hutool.http.HttpStatus;
|
import jakarta.servlet.ServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpSession;
|
import lombok.AccessLevel;
|
import lombok.NoArgsConstructor;
|
import org.springframework.http.MediaType;
|
import org.springframework.util.LinkedCaseInsensitiveMap;
|
import org.springframework.web.context.request.RequestAttributes;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import java.io.IOException;
|
import java.net.URLDecoder;
|
import java.net.URLEncoder;
|
import java.nio.charset.StandardCharsets;
|
import java.util.Collections;
|
import java.util.Enumeration;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 客户端工具类,提供获取请求参数、响应处理、头部信息等常用操作
|
*
|
* @author ruoyi
|
*/
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
public class ServletUtils extends JakartaServletUtil {
|
|
/**
|
* 获取指定名称的 String 类型的请求参数
|
*
|
* @param name 参数名
|
* @return 参数值
|
*/
|
public static String getParameter(String name) {
|
return getRequest().getParameter(name);
|
}
|
|
/**
|
* 获取指定名称的 String 类型的请求参数,若参数不存在,则返回默认值
|
*
|
* @param name 参数名
|
* @param defaultValue 默认值
|
* @return 参数值或默认值
|
*/
|
public static String getParameter(String name, String defaultValue) {
|
return Convert.toStr(getRequest().getParameter(name), defaultValue);
|
}
|
|
/**
|
* 获取指定名称的 Integer 类型的请求参数
|
*
|
* @param name 参数名
|
* @return 参数值
|
*/
|
public static Integer getParameterToInt(String name) {
|
return Convert.toInt(getRequest().getParameter(name));
|
}
|
|
/**
|
* 获取指定名称的 Integer 类型的请求参数,若参数不存在,则返回默认值
|
*
|
* @param name 参数名
|
* @param defaultValue 默认值
|
* @return 参数值或默认值
|
*/
|
public static Integer getParameterToInt(String name, Integer defaultValue) {
|
return Convert.toInt(getRequest().getParameter(name), defaultValue);
|
}
|
|
/**
|
* 获取指定名称的 Boolean 类型的请求参数
|
*
|
* @param name 参数名
|
* @return 参数值
|
*/
|
public static Boolean getParameterToBool(String name) {
|
return Convert.toBool(getRequest().getParameter(name));
|
}
|
|
/**
|
* 获取指定名称的 Boolean 类型的请求参数,若参数不存在,则返回默认值
|
*
|
* @param name 参数名
|
* @param defaultValue 默认值
|
* @return 参数值或默认值
|
*/
|
public static Boolean getParameterToBool(String name, Boolean defaultValue) {
|
return Convert.toBool(getRequest().getParameter(name), defaultValue);
|
}
|
|
/**
|
* 获取所有请求参数(以 Map 的形式返回)
|
*
|
* @param request 请求对象{@link ServletRequest}
|
* @return 请求参数的 Map,键为参数名,值为参数值数组
|
*/
|
public static Map<String, String[]> getParams(ServletRequest request) {
|
final Map<String, String[]> map = request.getParameterMap();
|
return Collections.unmodifiableMap(map);
|
}
|
|
/**
|
* 获取所有请求参数(以 Map 的形式返回,值为字符串形式的拼接)
|
*
|
* @param request 请求对象{@link ServletRequest}
|
* @return 请求参数的 Map,键为参数名,值为拼接后的字符串
|
*/
|
public static Map<String, String> getParamMap(ServletRequest request) {
|
Map<String, String> params = new HashMap<>();
|
for (Map.Entry<String, String[]> entry : getParams(request).entrySet()) {
|
params.put(entry.getKey(), StringUtils.join(entry.getValue(), StringUtils.SEPARATOR));
|
}
|
return params;
|
}
|
|
/**
|
* 获取当前 HTTP 请求对象
|
*
|
* @return 当前 HTTP 请求对象
|
*/
|
public static HttpServletRequest getRequest() {
|
try {
|
return getRequestAttributes().getRequest();
|
} catch (Exception e) {
|
return null;
|
}
|
}
|
|
/**
|
* 获取当前 HTTP 响应对象
|
*
|
* @return 当前 HTTP 响应对象
|
*/
|
public static HttpServletResponse getResponse() {
|
try {
|
return getRequestAttributes().getResponse();
|
} catch (Exception e) {
|
return null;
|
}
|
}
|
|
/**
|
* 获取当前请求的 HttpSession 对象
|
* <p>
|
* 如果当前请求已经关联了一个会话(即已经存在有效的 session ID),
|
* 则返回该会话对象;如果没有关联会话,则会创建一个新的会话对象并返回。
|
* <p>
|
* HttpSession 用于存储会话级别的数据,如用户登录信息、购物车内容等,
|
* 可以在多个请求之间共享会话数据
|
*
|
* @return 当前请求的 HttpSession 对象
|
*/
|
public static HttpSession getSession() {
|
return getRequest().getSession();
|
}
|
|
/**
|
* 获取当前请求的请求属性
|
*
|
* @return {@link ServletRequestAttributes} 请求属性对象
|
*/
|
public static ServletRequestAttributes getRequestAttributes() {
|
try {
|
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
return (ServletRequestAttributes) attributes;
|
} catch (Exception e) {
|
return null;
|
}
|
}
|
|
/**
|
* 获取指定请求头的值,如果头部为空则返回空字符串
|
*
|
* @param request 请求对象
|
* @param name 头部名称
|
* @return 头部值
|
*/
|
public static String getHeader(HttpServletRequest request, String name) {
|
String value = request.getHeader(name);
|
if (StringUtils.isEmpty(value)) {
|
return StringUtils.EMPTY;
|
}
|
return urlDecode(value);
|
}
|
|
/**
|
* 获取所有请求头的 Map,键为头部名称,值为头部值
|
*
|
* @param request 请求对象
|
* @return 请求头的 Map
|
*/
|
public static Map<String, String> getHeaders(HttpServletRequest request) {
|
Map<String, String> map = new LinkedCaseInsensitiveMap<>();
|
Enumeration<String> enumeration = request.getHeaderNames();
|
if (enumeration != null) {
|
while (enumeration.hasMoreElements()) {
|
String key = enumeration.nextElement();
|
String value = request.getHeader(key);
|
map.put(key, value);
|
}
|
}
|
return map;
|
}
|
|
/**
|
* 将字符串渲染到客户端(以 JSON 格式返回)
|
*
|
* @param response 渲染对象
|
* @param string 待渲染的字符串
|
*/
|
public static void renderString(HttpServletResponse response, String string) {
|
try {
|
response.setStatus(HttpStatus.HTTP_OK);
|
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
|
response.getWriter().print(string);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 判断当前请求是否为 Ajax 异步请求
|
*
|
* @param request 请求对象
|
* @return 是否为 Ajax 请求
|
*/
|
public static boolean isAjaxRequest(HttpServletRequest request) {
|
|
// 判断 Accept 头部是否包含 application/json
|
String accept = request.getHeader("accept");
|
if (accept != null && accept.contains(MediaType.APPLICATION_JSON_VALUE)) {
|
return true;
|
}
|
|
// 判断 X-Requested-With 头部是否包含 XMLHttpRequest
|
String xRequestedWith = request.getHeader("X-Requested-With");
|
if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
|
return true;
|
}
|
|
// 判断 URI 后缀是否为 .json 或 .xml
|
String uri = request.getRequestURI();
|
if (StringUtils.equalsAnyIgnoreCase(uri, ".json", ".xml")) {
|
return true;
|
}
|
|
// 判断请求参数 __ajax 是否为 json 或 xml
|
String ajax = request.getParameter("__ajax");
|
return StringUtils.equalsAnyIgnoreCase(ajax, "json", "xml");
|
}
|
|
/**
|
* 获取客户端 IP 地址
|
*
|
* @return 客户端 IP 地址
|
*/
|
public static String getClientIP() {
|
return getClientIP(getRequest());
|
}
|
|
/**
|
* 对内容进行 URL 编码
|
*
|
* @param str 内容
|
* @return 编码后的内容
|
*/
|
public static String urlEncode(String str) {
|
return URLEncoder.encode(str, StandardCharsets.UTF_8);
|
}
|
|
/**
|
* 对内容进行 URL 解码
|
*
|
* @param str 内容
|
* @return 解码后的内容
|
*/
|
public static String urlDecode(String str) {
|
return URLDecoder.decode(str, StandardCharsets.UTF_8);
|
}
|
|
}
|