dengzedong
2024-12-31 f20e755c36d40d3474b6866b4f1b006cb128ef75
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.common.util.servlet;
H 2
3 import cn.hutool.core.io.IoUtil;
4 import cn.hutool.core.util.StrUtil;
5 import cn.hutool.extra.servlet.ServletUtil;
6 import com.iailab.framework.common.util.json.JsonUtils;
7 import org.springframework.http.MediaType;
8 import org.springframework.web.context.request.RequestAttributes;
9 import org.springframework.web.context.request.RequestContextHolder;
10 import org.springframework.web.context.request.ServletRequestAttributes;
11
12 import javax.servlet.ServletRequest;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import java.io.IOException;
16 import java.net.URLEncoder;
17 import java.util.Map;
18
19 /**
20  * 客户端工具类
21  *
22  * @author iailab
23  */
24 public class ServletUtils {
25
26     /**
27      * 返回 JSON 字符串
28      *
29      * @param response 响应
30      * @param object   对象,会序列化成 JSON 字符串
31      */
32     @SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE,否则会乱码
33     public static void writeJSON(HttpServletResponse response, Object object) {
34         String content = JsonUtils.toJsonString(object);
35         ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE);
36     }
37
38     /**
39      * 返回附件
40      *
41      * @param response 响应
42      * @param filename 文件名
43      * @param content  附件内容
44      */
45     public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException {
46         // 设置 header 和 contentType
47         response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
48         response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
49         // 输出附件
50         IoUtil.write(response.getOutputStream(), false, content);
51     }
52
53     /**
54      * @param request 请求
55      * @return ua
56      */
57     public static String getUserAgent(HttpServletRequest request) {
58         String ua = request.getHeader("User-Agent");
59         return ua != null ? ua : "";
60     }
61
62     /**
63      * 获得请求
64      *
65      * @return HttpServletRequest
66      */
67     public static HttpServletRequest getRequest() {
68         RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
69         if (!(requestAttributes instanceof ServletRequestAttributes)) {
70             return null;
71         }
72         return ((ServletRequestAttributes) requestAttributes).getRequest();
73     }
74
75     public static String getUserAgent() {
76         HttpServletRequest request = getRequest();
77         if (request == null) {
78             return null;
79         }
80         return getUserAgent(request);
81     }
82
83     public static String getClientIP() {
84         HttpServletRequest request = getRequest();
85         if (request == null) {
86             return null;
87         }
88         return ServletUtil.getClientIP(request);
89     }
90
91     public static boolean isJsonRequest(ServletRequest request) {
92         return StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE);
93     }
94
95     public static String getBody(HttpServletRequest request) {
96         // 只有在 json 请求在读取,因为只有 CacheRequestBodyFilter 才会进行缓存,支持重复读取
97         if (isJsonRequest(request)) {
98             return ServletUtil.getBody(request);
99         }
100         return null;
101     }
102
103     public static byte[] getBodyBytes(HttpServletRequest request) {
104         // 只有在 json 请求在读取,因为只有 CacheRequestBodyFilter 才会进行缓存,支持重复读取
105         if (isJsonRequest(request)) {
106             return ServletUtil.getBodyBytes(request);
107         }
108         return null;
109     }
110
111     public static String getClientIP(HttpServletRequest request) {
112         return ServletUtil.getClientIP(request);
113     }
114
115     public static Map<String, String> getParamMap(HttpServletRequest request) {
116         return ServletUtil.getParamMap(request);
117     }
118
119 }