|
|
|
@ -1,13 +1,9 @@
|
|
|
|
|
package com.ruoyi.common.core.utils;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
import com.ruoyi.common.core.constant.Constants;
|
|
|
|
|
import com.ruoyi.common.core.domain.R;
|
|
|
|
|
import com.ruoyi.common.core.text.Convert;
|
|
|
|
|
import jakarta.servlet.ServletRequest;
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
@ -21,64 +17,62 @@ 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 com.alibaba.fastjson2.JSON;
|
|
|
|
|
import com.ruoyi.common.core.constant.Constants;
|
|
|
|
|
import com.ruoyi.common.core.domain.R;
|
|
|
|
|
import com.ruoyi.common.core.text.Convert;
|
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 客户端工具类
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
public class ServletUtils
|
|
|
|
|
{
|
|
|
|
|
public class ServletUtils {
|
|
|
|
|
/**
|
|
|
|
|
* 获取String参数
|
|
|
|
|
*/
|
|
|
|
|
public static String getParameter(String name)
|
|
|
|
|
{
|
|
|
|
|
public static String getParameter(String name) {
|
|
|
|
|
return getRequest().getParameter(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取String参数
|
|
|
|
|
*/
|
|
|
|
|
public static String getParameter(String name, String defaultValue)
|
|
|
|
|
{
|
|
|
|
|
public static String getParameter(String name, String defaultValue) {
|
|
|
|
|
return Convert.toStr(getRequest().getParameter(name), defaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取Integer参数
|
|
|
|
|
*/
|
|
|
|
|
public static Integer getParameterToInt(String name)
|
|
|
|
|
{
|
|
|
|
|
public static Integer getParameterToInt(String name) {
|
|
|
|
|
return Convert.toInt(getRequest().getParameter(name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取Integer参数
|
|
|
|
|
*/
|
|
|
|
|
public static Integer getParameterToInt(String name, Integer defaultValue)
|
|
|
|
|
{
|
|
|
|
|
public static Integer getParameterToInt(String name, Integer defaultValue) {
|
|
|
|
|
return Convert.toInt(getRequest().getParameter(name), defaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取Boolean参数
|
|
|
|
|
*/
|
|
|
|
|
public static Boolean getParameterToBool(String name)
|
|
|
|
|
{
|
|
|
|
|
public static Boolean getParameterToBool(String name) {
|
|
|
|
|
return Convert.toBool(getRequest().getParameter(name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取Boolean参数
|
|
|
|
|
*/
|
|
|
|
|
public static Boolean getParameterToBool(String name, Boolean defaultValue)
|
|
|
|
|
{
|
|
|
|
|
public static Boolean getParameterToBool(String name, Boolean defaultValue) {
|
|
|
|
|
return Convert.toBool(getRequest().getParameter(name), defaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -88,8 +82,7 @@ public class ServletUtils
|
|
|
|
|
* @param request 请求对象{@link ServletRequest}
|
|
|
|
|
* @return Map
|
|
|
|
|
*/
|
|
|
|
|
public static Map<String, String[]> getParams(ServletRequest request)
|
|
|
|
|
{
|
|
|
|
|
public static Map<String, String[]> getParams(ServletRequest request) {
|
|
|
|
|
final Map<String, String[]> map = request.getParameterMap();
|
|
|
|
|
return Collections.unmodifiableMap(map);
|
|
|
|
|
}
|
|
|
|
@ -100,11 +93,9 @@ public class ServletUtils
|
|
|
|
|
* @param request 请求对象{@link ServletRequest}
|
|
|
|
|
* @return Map
|
|
|
|
|
*/
|
|
|
|
|
public static Map<String, String> getParamMap(ServletRequest request)
|
|
|
|
|
{
|
|
|
|
|
public static Map<String, String> getParamMap(ServletRequest request) {
|
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
|
for (Map.Entry<String, String[]> entry : getParams(request).entrySet())
|
|
|
|
|
{
|
|
|
|
|
for (Map.Entry<String, String[]> entry : getParams(request).entrySet()) {
|
|
|
|
|
params.put(entry.getKey(), StringUtils.join(entry.getValue(), ","));
|
|
|
|
|
}
|
|
|
|
|
return params;
|
|
|
|
@ -113,14 +104,10 @@ public class ServletUtils
|
|
|
|
|
/**
|
|
|
|
|
* 获取request
|
|
|
|
|
*/
|
|
|
|
|
public static HttpServletRequest getRequest()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
public static HttpServletRequest getRequest() {
|
|
|
|
|
try {
|
|
|
|
|
return getRequestAttributes().getRequest();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -128,14 +115,10 @@ public class ServletUtils
|
|
|
|
|
/**
|
|
|
|
|
* 获取response
|
|
|
|
|
*/
|
|
|
|
|
public static HttpServletResponse getResponse()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
public static HttpServletResponse getResponse() {
|
|
|
|
|
try {
|
|
|
|
|
return getRequestAttributes().getResponse();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -143,42 +126,32 @@ public class ServletUtils
|
|
|
|
|
/**
|
|
|
|
|
* 获取session
|
|
|
|
|
*/
|
|
|
|
|
public static HttpSession getSession()
|
|
|
|
|
{
|
|
|
|
|
public static HttpSession getSession() {
|
|
|
|
|
return getRequest().getSession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ServletRequestAttributes getRequestAttributes()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
public static ServletRequestAttributes getRequestAttributes() {
|
|
|
|
|
try {
|
|
|
|
|
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
|
|
|
|
return (ServletRequestAttributes) attributes;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getHeader(HttpServletRequest request, String name)
|
|
|
|
|
{
|
|
|
|
|
public static String getHeader(HttpServletRequest request, String name) {
|
|
|
|
|
String value = request.getHeader(name);
|
|
|
|
|
if (StringUtils.isEmpty(value))
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isEmpty(value)) {
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
}
|
|
|
|
|
return urlDecode(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Map<String, String> getHeaders(HttpServletRequest request)
|
|
|
|
|
{
|
|
|
|
|
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())
|
|
|
|
|
{
|
|
|
|
|
if (enumeration != null) {
|
|
|
|
|
while (enumeration.hasMoreElements()) {
|
|
|
|
|
String key = enumeration.nextElement();
|
|
|
|
|
String value = request.getHeader(key);
|
|
|
|
|
map.put(key, value);
|
|
|
|
@ -191,45 +164,35 @@ public class ServletUtils
|
|
|
|
|
* 将字符串渲染到客户端
|
|
|
|
|
*
|
|
|
|
|
* @param response 渲染对象
|
|
|
|
|
* @param string 待渲染的字符串
|
|
|
|
|
* @param string 待渲染的字符串
|
|
|
|
|
*/
|
|
|
|
|
public static void renderString(HttpServletResponse response, String string)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
public static void renderString(HttpServletResponse response, String string) {
|
|
|
|
|
try {
|
|
|
|
|
response.setStatus(200);
|
|
|
|
|
response.setContentType("application/json");
|
|
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
|
|
response.getWriter().print(string);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否是Ajax异步请求
|
|
|
|
|
*
|
|
|
|
|
* @param request
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isAjaxRequest(HttpServletRequest request)
|
|
|
|
|
{
|
|
|
|
|
public static boolean isAjaxRequest(HttpServletRequest request) {
|
|
|
|
|
String accept = request.getHeader("accept");
|
|
|
|
|
if (accept != null && accept.contains("application/json"))
|
|
|
|
|
{
|
|
|
|
|
if (accept != null && accept.contains("application/json")) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String xRequestedWith = request.getHeader("X-Requested-With");
|
|
|
|
|
if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
|
|
|
|
|
{
|
|
|
|
|
if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String uri = request.getRequestURI();
|
|
|
|
|
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -243,14 +206,10 @@ public class ServletUtils
|
|
|
|
|
* @param str 内容
|
|
|
|
|
* @return 编码后的内容
|
|
|
|
|
*/
|
|
|
|
|
public static String urlEncode(String str)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
public static String urlEncode(String str) {
|
|
|
|
|
try {
|
|
|
|
|
return URLEncoder.encode(str, Constants.UTF8);
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedEncodingException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -261,14 +220,10 @@ public class ServletUtils
|
|
|
|
|
* @param str 内容
|
|
|
|
|
* @return 解码后的内容
|
|
|
|
|
*/
|
|
|
|
|
public static String urlDecode(String str)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
public static String urlDecode(String str) {
|
|
|
|
|
try {
|
|
|
|
|
return URLDecoder.decode(str, Constants.UTF8);
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedEncodingException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
return StringUtils.EMPTY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -277,11 +232,10 @@ public class ServletUtils
|
|
|
|
|
* 设置webflux模型响应
|
|
|
|
|
*
|
|
|
|
|
* @param response ServerHttpResponse
|
|
|
|
|
* @param value 响应内容
|
|
|
|
|
* @param value 响应内容
|
|
|
|
|
* @return Mono<Void>
|
|
|
|
|
*/
|
|
|
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value)
|
|
|
|
|
{
|
|
|
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value) {
|
|
|
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -289,12 +243,11 @@ public class ServletUtils
|
|
|
|
|
* 设置webflux模型响应
|
|
|
|
|
*
|
|
|
|
|
* @param response ServerHttpResponse
|
|
|
|
|
* @param code 响应状态码
|
|
|
|
|
* @param value 响应内容
|
|
|
|
|
* @param code 响应状态码
|
|
|
|
|
* @param value 响应内容
|
|
|
|
|
* @return Mono<Void>
|
|
|
|
|
*/
|
|
|
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code)
|
|
|
|
|
{
|
|
|
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code) {
|
|
|
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -302,28 +255,26 @@ public class ServletUtils
|
|
|
|
|
* 设置webflux模型响应
|
|
|
|
|
*
|
|
|
|
|
* @param response ServerHttpResponse
|
|
|
|
|
* @param status http状态码
|
|
|
|
|
* @param code 响应状态码
|
|
|
|
|
* @param value 响应内容
|
|
|
|
|
* @param status http状态码
|
|
|
|
|
* @param code 响应状态码
|
|
|
|
|
* @param value 响应内容
|
|
|
|
|
* @return Mono<Void>
|
|
|
|
|
*/
|
|
|
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code)
|
|
|
|
|
{
|
|
|
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code) {
|
|
|
|
|
return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置webflux模型响应
|
|
|
|
|
*
|
|
|
|
|
* @param response ServerHttpResponse
|
|
|
|
|
* @param response ServerHttpResponse
|
|
|
|
|
* @param contentType content-type
|
|
|
|
|
* @param status http状态码
|
|
|
|
|
* @param code 响应状态码
|
|
|
|
|
* @param value 响应内容
|
|
|
|
|
* @param status http状态码
|
|
|
|
|
* @param code 响应状态码
|
|
|
|
|
* @param value 响应内容
|
|
|
|
|
* @return Mono<Void>
|
|
|
|
|
*/
|
|
|
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code)
|
|
|
|
|
{
|
|
|
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code) {
|
|
|
|
|
response.setStatusCode(status);
|
|
|
|
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
|
|
|
|
R<?> result = R.fail(code, value.toString());
|
|
|
|
|