懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 package com.jcdm.common.utils;
2
3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.net.URLDecoder;
6 import java.net.URLEncoder;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10 import javax.servlet.ServletRequest;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.HttpSession;
14 import org.springframework.web.context.request.RequestAttributes;
15 import org.springframework.web.context.request.RequestContextHolder;
16 import org.springframework.web.context.request.ServletRequestAttributes;
17 import com.jcdm.common.constant.Constants;
18 import com.jcdm.common.core.text.Convert;
19
20 /**
21  * 客户端工具类
22  * 
23  * @author jc
24  */
25 public class ServletUtils
26 {
27     /**
28      * 获取String参数
29      */
30     public static String getParameter(String name)
31     {
32         return getRequest().getParameter(name);
33     }
34
35     /**
36      * 获取String参数
37      */
38     public static String getParameter(String name, String defaultValue)
39     {
40         return Convert.toStr(getRequest().getParameter(name), defaultValue);
41     }
42
43     /**
44      * 获取Integer参数
45      */
46     public static Integer getParameterToInt(String name)
47     {
48         return Convert.toInt(getRequest().getParameter(name));
49     }
50
51     /**
52      * 获取Integer参数
53      */
54     public static Integer getParameterToInt(String name, Integer defaultValue)
55     {
56         return Convert.toInt(getRequest().getParameter(name), defaultValue);
57     }
58
59     /**
60      * 获取Boolean参数
61      */
62     public static Boolean getParameterToBool(String name)
63     {
64         return Convert.toBool(getRequest().getParameter(name));
65     }
66
67     /**
68      * 获取Boolean参数
69      */
70     public static Boolean getParameterToBool(String name, Boolean defaultValue)
71     {
72         return Convert.toBool(getRequest().getParameter(name), defaultValue);
73     }
74
75     /**
76      * 获得所有请求参数
77      *
78      * @param request 请求对象{@link ServletRequest}
79      * @return Map
80      */
81     public static Map<String, String[]> getParams(ServletRequest request)
82     {
83         final Map<String, String[]> map = request.getParameterMap();
84         return Collections.unmodifiableMap(map);
85     }
86
87     /**
88      * 获得所有请求参数
89      *
90      * @param request 请求对象{@link ServletRequest}
91      * @return Map
92      */
93     public static Map<String, String> getParamMap(ServletRequest request)
94     {
95         Map<String, String> params = new HashMap<>();
96         for (Map.Entry<String, String[]> entry : getParams(request).entrySet())
97         {
98             params.put(entry.getKey(), StringUtils.join(entry.getValue(), ","));
99         }
100         return params;
101     }
102
103     /**
104      * 获取request
105      */
106     public static HttpServletRequest getRequest()
107     {
108         return getRequestAttributes().getRequest();
109     }
110
111     /**
112      * 获取response
113      */
114     public static HttpServletResponse getResponse()
115     {
116         return getRequestAttributes().getResponse();
117     }
118
119     /**
120      * 获取session
121      */
122     public static HttpSession getSession()
123     {
124         return getRequest().getSession();
125     }
126
127     public static ServletRequestAttributes getRequestAttributes()
128     {
129         RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
130         return (ServletRequestAttributes) attributes;
131     }
132
133     /**
134      * 将字符串渲染到客户端
135      * 
136      * @param response 渲染对象
137      * @param string 待渲染的字符串
138      */
139     public static void renderString(HttpServletResponse response, String string)
140     {
141         try
142         {
143             response.setStatus(200);
144             response.setContentType("application/json");
145             response.setCharacterEncoding("utf-8");
146             response.getWriter().print(string);
147         }
148         catch (IOException e)
149         {
150             e.printStackTrace();
151         }
152     }
153
154     /**
155      * 是否是Ajax异步请求
156      * 
157      * @param request
158      */
159     public static boolean isAjaxRequest(HttpServletRequest request)
160     {
161         String accept = request.getHeader("accept");
162         if (accept != null && accept.contains("application/json"))
163         {
164             return true;
165         }
166
167         String xRequestedWith = request.getHeader("X-Requested-With");
168         if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
169         {
170             return true;
171         }
172
173         String uri = request.getRequestURI();
174         if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
175         {
176             return true;
177         }
178
179         String ajax = request.getParameter("__ajax");
180         return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
181     }
182
183     /**
184      * 内容编码
185      * 
186      * @param str 内容
187      * @return 编码后的内容
188      */
189     public static String urlEncode(String str)
190     {
191         try
192         {
193             return URLEncoder.encode(str, Constants.UTF8);
194         }
195         catch (UnsupportedEncodingException e)
196         {
197             return StringUtils.EMPTY;
198         }
199     }
200
201     /**
202      * 内容解码
203      * 
204      * @param str 内容
205      * @return 解码后的内容
206      */
207     public static String urlDecode(String str)
208     {
209         try
210         {
211             return URLDecoder.decode(str, Constants.UTF8);
212         }
213         catch (UnsupportedEncodingException e)
214         {
215             return StringUtils.EMPTY;
216         }
217     }
218 }