();
1042 | for (int i = 0; i < paraNames.size(); i++) {
1043 | BeanEntity entity = new BeanEntity();
1044 | entity.setFieldName(paraNames.get(i));
1045 | entity.setFieldAnnotations(paraAnnotations[i]);
1046 | entity.setFieldType(types[i]);
1047 | entitys.add(entity);
1048 | }
1049 | paramMap.put(method, entitys);
1050 | return entitys;
1051 | } catch (Exception e) {
1052 | e.printStackTrace();
1053 | }
1054 | return null;
1055 | }
1056 |
1057 | /**
1058 | *
1059 | *
1060 | * 获取方法的参数名
1061 | *
1062 | *
1063 | * @param m
1064 | * @return
1065 | */
1066 | public static List getMethodParaNames(Method method) {
1067 | if (StringUtil.isNullOrEmpty(method.getParameters())) {
1068 | return null;
1069 | }
1070 | List paramNames = new ArrayList();
1071 | for (Parameter parameter : method.getParameters()) {
1072 | paramNames.add(parameter.getName());
1073 | }
1074 | return paramNames;
1075 | }
1076 |
1077 | }
--------------------------------------------------------------------------------
/src/main/java/org/coody/framework/util/RequestUtil.java:
--------------------------------------------------------------------------------
1 | package org.coody.framework.util;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.net.URLDecoder;
6 | import java.util.ArrayList;
7 | import java.util.Enumeration;
8 | import java.util.HashMap;
9 | import java.util.List;
10 | import java.util.Map;
11 |
12 | import javax.servlet.http.Cookie;
13 | import javax.servlet.http.HttpServletRequest;
14 |
15 | import org.coody.framework.entity.BaseModel;
16 | import org.coody.framework.entity.BeanEntity;
17 |
18 | /**
19 | * @remark HTTP工具类。
20 | * @author Coody
21 | * @email 644556636@qq.com
22 | * @blog 54sb.org
23 | */
24 | public class RequestUtil {
25 |
26 |
27 | public static final String user_session = "curr_login_user";
28 |
29 | public static void setUser(HttpServletRequest request, Object user) {
30 | request.getSession().setAttribute(user_session, user);
31 | }
32 |
33 | @SuppressWarnings("unchecked")
34 | public static T getUser(HttpServletRequest request) {
35 | return (T) request.getSession().getAttribute(user_session);
36 | }
37 |
38 |
39 | public static boolean isUserLogin(HttpServletRequest request) {
40 | Object obj = getUser(request);
41 | if (!StringUtil.isNullOrEmpty(obj)) {
42 | return true;
43 | }
44 | return false;
45 | }
46 |
47 | public static void setCode(HttpServletRequest request, Object code) {
48 | request.getSession().setAttribute("sys_ver_code", code);
49 | }
50 |
51 | @SuppressWarnings("unchecked")
52 | public static T getCode(HttpServletRequest request) {
53 | return (T) request.getSession().getAttribute("sys_ver_code");
54 | }
55 |
56 | public static String getIpAddr(HttpServletRequest request) {
57 | String ip=request.getRemoteAddr();
58 | if(!isInvialIp(ip)){
59 | return ip;
60 | }
61 | ip = request.getHeader("X-Real-IP");
62 | if(!isInvialIp(ip)){
63 | return ip;
64 | }
65 | ip = request.getHeader("X-Forwarded-For");
66 | if(!isInvialIp(ip)){
67 | // 多次反向代理后会有多个IP值,第一个为真实IP。
68 | int index = ip.indexOf(',');
69 | if (index != -1) {
70 | return ip.substring(0, index);
71 | }
72 | return ip;
73 | }
74 | return "未知";
75 | }
76 |
77 | private static boolean isInvialIp(String ip){
78 | if(ip.equals("127.0.0.1")||ip.equalsIgnoreCase("localhost")){
79 | return true;
80 | }
81 | if (StringUtil.isNullOrEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
82 | return true;
83 | }
84 | return false;
85 | }
86 | public static String getRequestUri(HttpServletRequest request) {
87 | String uri = request.getServletPath();
88 |
89 | String projectName = request.getContextPath();
90 | if (projectName != null && !projectName.trim().equals("")) {
91 | uri = uri.replace(projectName, "/");
92 | }
93 | uri = uri.replace("../", "/");
94 | while (uri.indexOf("//") > -1) {
95 | uri = uri.replace("//", "/");
96 | }
97 | return uri;
98 | }
99 |
100 | public static String getURLSuffix(HttpServletRequest request) {
101 | String url = request.getServletPath();
102 | String[] tab = url.split("\\.");
103 | if (tab.length > 1) {
104 | String suffix= tab[tab.length - 1];
105 | if(!StringUtil.isNullOrEmpty(suffix)){
106 | request.setAttribute("suffix", suffix);
107 | }
108 | return suffix;
109 | }
110 | return "";
111 | }
112 |
113 | /**
114 | * 根据Request获取Model。排除指定参数
115 | *
116 | * @param request
117 | * 请求对象
118 | * @param obj
119 | * 实例化的Model对象
120 | * @param paraArgs
121 | * 指定参数
122 | * @return
123 | */
124 | public static T getBeanRemove(HttpServletRequest request,
125 | String paraName, Object obj, String... paraArgs) {
126 | return getBean(request, obj, null, paraName, null, true, paraArgs);
127 | }
128 |
129 | /**
130 | * 根据Request获取Model。接受指定参数
131 | *
132 | * @param request
133 | * 请求对象
134 | * @param obj
135 | * 实例化的Model对象
136 | * @param paraArgs
137 | * 指定参数
138 | * @return
139 | */
140 | public static T getBeanAccept(HttpServletRequest request,
141 | String paraName, Object obj, String... paraArgs) {
142 | return getBean(request, obj, null, paraName, null, false, paraArgs);
143 | }
144 |
145 | /**
146 | * 根据Request获取Model所有参数
147 | *
148 | * @param request
149 | * 请求对象
150 | * @param obj
151 | * 实例化的Model对象
152 | * @return
153 | */
154 | public static T getBeanAll(HttpServletRequest request, String paraName,
155 | Object obj) {
156 | return getBean(request, obj, null, paraName, null, true, null);
157 | }
158 |
159 | @SuppressWarnings("unchecked")
160 | private static T getBean(HttpServletRequest request, Object obj,
161 | List fields, String baseName, String firstSuffix,
162 | Boolean isReplace, String[] paraArgs) {
163 | try {
164 | if (obj instanceof Class) {
165 | obj = ((Class>) obj).newInstance();
166 | }
167 | firstSuffix = StringUtil.isNullOrEmpty(firstSuffix) ? ""
168 | : (firstSuffix + ".");
169 | isReplace = StringUtil.isNullOrEmpty(isReplace) ? false : isReplace;
170 | baseName = StringUtil.isNullOrEmpty(baseName) ? ""
171 | : (baseName + ".");
172 | List paras = null;
173 | if (!StringUtil.isNullOrEmpty(paraArgs)) {
174 | paras = new ArrayList();
175 | for (String tmp : paraArgs) {
176 | if (StringUtil.isNullOrEmpty(tmp)) {
177 | continue;
178 | }
179 | String[] tab = tmp.split("\\.");
180 | for (String tmpTab : tab) {
181 | paras.add(tmpTab);
182 | }
183 | }
184 | }
185 | if (StringUtil.isNullOrEmpty(fields)) {
186 | // 获取对象字段属性
187 | fields = PropertUtil.getBeanFields(obj);
188 | }
189 | firstSuffix = (firstSuffix == null) ? "" : firstSuffix;
190 | Object childObj, paraValue = null;
191 | String paraName = null;
192 | for (BeanEntity entity : fields) {
193 | try {
194 | paraName = firstSuffix + baseName + entity.getFieldName();
195 | if (!StringUtil.isNullOrEmpty(paras)) {
196 | if (isReplace) {
197 | if (paras.contains(paraName)) {
198 | continue;
199 | }
200 | }
201 | if (!isReplace) {
202 | if (!paras.contains(paraName)) {
203 | continue;
204 | }
205 | }
206 | }
207 | if (BaseModel.class.isAssignableFrom(entity.getFieldType())) {
208 | childObj = entity.getFieldType().newInstance();
209 | childObj = getBean(request, childObj, null, paraName,
210 | firstSuffix, isReplace, paraArgs);
211 | PropertUtil.setProperties(obj, entity.getFieldName(),
212 | childObj);
213 | continue;
214 | }
215 | paraValue = request.getParameter(paraName);
216 | if (paraValue==null) {
217 | continue;
218 | }
219 | PropertUtil.setProperties(obj, entity.getFieldName(),
220 | paraValue);
221 |
222 | } catch (Exception e) {
223 | }
224 | }
225 | return (T) obj;
226 | } catch (Exception e) {
227 |
228 |
229 | }
230 | return null;
231 | }
232 |
233 | @SuppressWarnings("unchecked")
234 | public static void keepParas(HttpServletRequest request) {
235 | Enumeration paras = request.getParameterNames();
236 | if (StringUtil.isNullOrEmpty(paras)) {
237 | return;
238 | }
239 | while (paras.hasMoreElements()) {
240 | try {
241 | String string = (String) paras.nextElement();
242 | if (StringUtil.isNullOrEmpty(string)) {
243 | continue;
244 | }
245 | request.setAttribute(string.replace(".", "_"),
246 | request.getParameter(string));
247 | } catch (Exception e) {
248 |
249 | }
250 |
251 | }
252 | }
253 |
254 | public static String getRequestURI(HttpServletRequest request) {
255 | String uri = request.getRequestURI();
256 | String projectName = request.getContextPath();
257 | if (projectName != null && !projectName.trim().equals("")) {
258 | uri = uri.replace(projectName, "/");
259 | }
260 | uri = uri.replace("../", "/");
261 | while (uri.indexOf("//") > -1) {
262 | uri = uri.replace("//", "/");
263 | }
264 | return uri;
265 | }
266 |
267 | /**
268 | * 获取POST请求参数中数据
269 | *
270 | * @param request
271 | * @throws IOException
272 | */
273 | public static String getPostContent(HttpServletRequest request) {
274 | String content = null;
275 | try {
276 | content = inputStream2String(request.getInputStream());
277 | content=URLDecoder.decode(content,"UTF-8");
278 | } catch (Exception e) {
279 |
280 | }
281 | return content;
282 | }
283 | public static String inputStream2String(InputStream in) throws IOException {
284 | StringBuffer out = new StringBuffer();
285 | byte[] b = new byte[4096];
286 | for (int n; (n = in.read(b)) != -1;) {
287 | out.append(new String(b, 0, n));
288 | }
289 | return out.toString();
290 | }
291 |
292 | public static String getRequestCookies(HttpServletRequest request) {
293 | Cookie[] cookies = request.getCookies();
294 | if (StringUtil.isNullOrEmpty(cookies)) {
295 | return null;
296 | }
297 | StringBuilder sb = new StringBuilder();
298 | for (Cookie cook : cookies) {
299 | sb.append(" "+cook.getName()).append("=").append(cook.getValue())
300 | .append(";");
301 | }
302 | return sb.toString();
303 | }
304 | @SuppressWarnings("unchecked")
305 | public static Map getParas(HttpServletRequest request) {
306 | Map map=request.getParameterMap();
307 | if(StringUtil.isNullOrEmpty(map)){
308 | return null;
309 | }
310 | Map paraMap=new HashMap();
311 | Cookie[] cooks=request.getCookies();
312 | if(!StringUtil.isNullOrEmpty(cooks)) {
313 | for(Cookie cook:cooks){
314 | try {
315 | paraMap.put(cook.getName(), cook.getValue());
316 | } catch (Exception e) {
317 |
318 | }
319 | }
320 | }
321 |
322 | for (String key:map.keySet()) {
323 | String[] values=map.get(key);
324 | if(StringUtil.isNullOrEmpty(values)){
325 | continue;
326 | }
327 | if(values.length==1){
328 | paraMap.put(key, values[0]);
329 | continue;
330 | }
331 | paraMap.put(key, values);
332 | }
333 | return paraMap;
334 |
335 |
336 | }
337 | public static T parseRequestToBean(HttpServletRequest req,Class clazz){
338 | Map paraMap=getParas(req);
339 | return parseMapToBean(paraMap, clazz);
340 | }
341 | public static T parseMapToBean(Map paraMap,Class clazz){
342 | if(StringUtil.isNullOrEmpty(paraMap)){
343 | return null;
344 | }
345 | try {
346 | T bean=clazz.newInstance();
347 | for(String key:paraMap.keySet()){
348 | try {
349 | PropertUtil.setProperties(bean, key, paraMap.get(key));
350 | } catch (Exception e) {
351 |
352 | }
353 | }
354 | return bean;
355 | } catch (Exception e) {
356 |
357 | }
358 | return null;
359 | }
360 | public static String loadBasePath(HttpServletRequest request) {
361 | String path = request.getContextPath();
362 | String basePath = request.getScheme()
363 | + "://"
364 | + request.getServerName()
365 | + (request.getServerPort() == 80 ? "" : ":"
366 | + request.getServerPort()) + path + "/";
367 | basePath=basePath.replace("http:", "");
368 | basePath=basePath.replace("https:", "");
369 | request.getSession().setAttribute("basePath", basePath);
370 | request.setAttribute("basePath", basePath);
371 | return basePath;
372 | }
373 | public static void main(String[] args) {
374 |
375 | }
376 |
377 | }
378 |
--------------------------------------------------------------------------------
/src/main/java/org/coody/framework/util/StringUtil.java:
--------------------------------------------------------------------------------
1 | package org.coody.framework.util;
2 |
3 | import java.math.BigDecimal;
4 | import java.util.ArrayList;
5 | import java.util.Arrays;
6 | import java.util.Collection;
7 | import java.util.List;
8 | import java.util.Map;
9 | import java.util.Set;
10 | import java.util.Vector;
11 | import java.util.regex.Matcher;
12 | import java.util.regex.Pattern;
13 |
14 | import javax.servlet.http.HttpServletRequest;
15 |
16 | //import oracle.sql.CLOB;
17 |
18 | public class StringUtil {
19 |
20 |
21 | public static Integer[] getIntegerParas(Object[] objs) {
22 | if (isNullOrEmpty(objs)) {
23 | return null;
24 | }
25 | Integer[] ints = new Integer[objs.length];
26 | for (int i = 0; i < objs.length; i++) {
27 | try {
28 | ints[i] = Integer.valueOf(objs[i].toString());
29 | } catch (Exception e) {
30 | }
31 | }
32 | return ints;
33 | }
34 |
35 | /**
36 | * 生成指定数目字符串按分隔符分割
37 | *
38 | * @param baseStr
39 | * @param mosaicChr
40 | * @param size
41 | * @return
42 | */
43 | public static String getByMosaicChr(String baseStr, String mosaicChr, Integer size) {
44 | List list = new ArrayList();
45 | for (int i = 0; i < size; i++) {
46 | if (isNullOrEmpty(baseStr)) {
47 | continue;
48 | }
49 | list.add(baseStr);
50 | }
51 | return collectionMosaic(list, mosaicChr);
52 | }
53 |
54 | /**
55 | * 根据分割符将字符串分割成String数组
56 | *
57 | * @param src
58 | * 源字符串
59 | * @param separator
60 | * 分隔�?
61 | * @return String数组
62 | */
63 | public static String[] splitToStringArray(String src, String separator) {
64 | Vector splitArrays = new Vector();
65 | int i = 0;
66 | int j = 0;
67 | while (i <= src.length()) {
68 | j = src.indexOf(separator, i);
69 | if (j < 0) {
70 | j = src.length();
71 | }
72 | splitArrays.addElement(src.substring(i, j));
73 | i = j + 1;
74 | }
75 | int size = splitArrays.size();
76 | String[] array = new String[size];
77 | System.arraycopy(splitArrays.toArray(), 0, array, 0, size);
78 | return array;
79 | }
80 |
81 | /**
82 | * 根据分割符将字符串分割成Integer数组
83 | *
84 | * @param src
85 | * 源字符串
86 | * @param separator
87 | * 分隔�?
88 | * @return Integer数组
89 | */
90 | public static Integer[] splitToIntgArray(String src, String separator) {
91 | String[] arr = splitToStringArray(src, separator);
92 | Integer[] intArr = new Integer[arr.length];
93 | for (int i = 0; i < arr.length; i++) {
94 | intArr[i] = Integer.valueOf(arr[i]);
95 | }
96 | return intArr;
97 | }
98 |
99 | /**
100 | * 根据分隔符将字符串分割成int数组
101 | *
102 | * @param src
103 | * 源字符串
104 | * @param separator
105 | * 分隔�?
106 | * @return int数组
107 | */
108 | public static int[] splitToIntArray(String src, String separator) {
109 | String[] arr = splitToStringArray(src, separator);
110 | int[] intArr = new int[arr.length];
111 | for (int i = 0; i < arr.length; i++) {
112 | intArr[i] = Integer.parseInt(arr[i]);
113 | }
114 | return intArr;
115 | }
116 |
117 | public static String getInPara(Integer size) {
118 | return getByMosaicChr("?", ",", size);
119 |
120 | }
121 |
122 | public static String textCutCenter(String allTxt, String firstTxt, String lastTxt) {
123 | try {
124 | String tmp = "";
125 | int n1 = allTxt.indexOf(firstTxt);
126 | if (n1 == -1) {
127 | return "";
128 | }
129 | tmp = allTxt.substring(n1 + firstTxt.length(), allTxt.length());
130 | int n2 = tmp.indexOf(lastTxt);
131 | if (n2 == -1) {
132 | return "";
133 | }
134 | tmp = tmp.substring(0, n2);
135 | return tmp;
136 | } catch (Exception e) {
137 | return "";
138 | }
139 | }
140 |
141 | public static List textCutCenters(String allTxt, String firstTxt, String lastTxt) {
142 | try {
143 | List results = new ArrayList();
144 | while (allTxt.contains(firstTxt)) {
145 | int n = allTxt.indexOf(firstTxt);
146 | allTxt = allTxt.substring(n + firstTxt.length(), allTxt.length());
147 | n = allTxt.indexOf(lastTxt);
148 | if (n == -1) {
149 | return results;
150 | }
151 | String result = allTxt.substring(0, n);
152 | results.add(result);
153 | allTxt = allTxt.substring(n + firstTxt.length(), allTxt.length());
154 | }
155 | return results;
156 | } catch (Exception e) {
157 | return null;
158 | }
159 | }
160 |
161 | public static String convertToUnicode(String source) {
162 | String result = "";
163 | char[] chrs = source.toCharArray();
164 | for (int i = 0; i < chrs.length; i++) {
165 | result += "" + Character.codePointAt(chrs, i);
166 | }
167 | return result;
168 | }
169 |
170 | public static Integer toInteger(Object obj) {
171 | if (isNullOrEmpty(obj)) {
172 | return null;
173 | }
174 | try {
175 | return Integer.valueOf(obj.toString());
176 | } catch (Exception e) {
177 | return null;
178 | }
179 | }
180 |
181 | public static String toString(Object obj) {
182 | if (isNullOrEmpty(obj)) {
183 | return null;
184 | }
185 | try {
186 | return String.valueOf(obj.toString());
187 | } catch (Exception e) {
188 | return null;
189 | }
190 | }
191 |
192 | public static Double toDouble(Object obj) {
193 | if (isNullOrEmpty(obj)) {
194 | return null;
195 | }
196 | try {
197 | return Double.valueOf(obj.toString());
198 | } catch (Exception e) {
199 | return null;
200 | }
201 | }
202 |
203 | public static Float toFloat(Object obj) {
204 | if (isNullOrEmpty(obj)) {
205 | return null;
206 | }
207 | try {
208 | return Float.valueOf(obj.toString());
209 | } catch (Exception e) {
210 | return null;
211 | }
212 | }
213 |
214 | public static Long toLong(Object obj) {
215 | if (isNullOrEmpty(obj)) {
216 | return null;
217 | }
218 | try {
219 | return Long.valueOf(obj.toString());
220 | } catch (Exception e) {
221 | return null;
222 | }
223 | }
224 |
225 | public static Integer getRanDom(int start, int end) {
226 | return (int) (Math.random() * (end - start + 1)) + start;
227 | }
228 |
229 | public static float getRanDom(Float start, Float end) {
230 | String str = String.valueOf(start);
231 | String[] tabs = str.split("\\.");
232 | Integer startLength = 1;
233 | if (tabs.length == 2) {
234 | startLength = tabs[1].length();
235 | }
236 | str = String.valueOf(end);
237 | tabs = str.split("\\.");
238 | Integer endLength = 1;
239 | if (tabs.length == 2) {
240 | endLength = tabs[1].length();
241 | }
242 | if (endLength > startLength) {
243 | startLength = endLength;
244 | }
245 | start = (float) (start * Math.pow(10, startLength));
246 | end = (float) (end * Math.pow(10, startLength));
247 | return (float) (getRanDom(start.intValue(), end.intValue()) / Math.pow(10, startLength));
248 | }
249 |
250 | public static String replaceBlank(String str) {
251 | String dest = "";
252 | if (str != null) {
253 | Pattern p = Pattern.compile("\\s*|\t|\r|\n");
254 | Matcher m = p.matcher(str);
255 | dest = m.replaceAll("");
256 | }
257 | return dest;
258 | }
259 |
260 | public static Boolean isMatcher(String val, String matcher) {
261 | Pattern p = Pattern.compile(matcher);
262 | Matcher m = p.matcher(val);
263 | return m.matches();
264 | }
265 |
266 | public static List matchExport(String context, String patten) {
267 | try {
268 | Integer index = 0;
269 | Pattern pattern = Pattern.compile(patten, Pattern.DOTALL);
270 | Matcher matcher = pattern.matcher(context);
271 | List results = new ArrayList();
272 | while (matcher.find(index)) {
273 | String tmp = matcher.group(1);
274 | index = matcher.end();
275 | if (isNullOrEmpty(tmp)) {
276 | continue;
277 | }
278 | results.add(tmp);
279 | }
280 | return results;
281 | } catch (Exception e) {
282 | return null;
283 | }
284 | }
285 |
286 | public static String matchExportFirst(String context, String patten) {
287 | try {
288 | Integer index = 0;
289 | Pattern pattern = Pattern.compile(patten, Pattern.DOTALL);
290 | Matcher matcher = pattern.matcher(context);
291 | while (matcher.find(index)) {
292 | String tmp = matcher.group(1);
293 | index = matcher.end();
294 | if (isNullOrEmpty(tmp)) {
295 | continue;
296 | }
297 | return tmp;
298 | }
299 | return null;
300 | } catch (Exception e) {
301 | return null;
302 | }
303 | }
304 | public static boolean isMobile(String mobile) {
305 | if (isNullOrEmpty(mobile)) {
306 | return false;
307 | }
308 | Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(17[^4,\\D])|(18[0,5-9]))\\d{8}$");
309 | Matcher m = p.matcher(mobile);
310 | return m.matches();
311 | }
312 |
313 | public static boolean isLegal(String str) {
314 | if (isNullOrEmpty(str)) {
315 | return false;
316 | }
317 | Pattern p = Pattern.compile("[A-Za-z0-9_]{3,16}");
318 | Matcher m = p.matcher(str);
319 | return m.matches();
320 | }
321 |
322 | public static boolean isEmail(String email) {
323 | if (isNullOrEmpty(email)) {
324 | return false;
325 | }
326 | Pattern p = Pattern.compile(
327 | "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
328 | Matcher m = p.matcher(email);
329 | return m.matches();
330 | }
331 |
332 | public static boolean isMd5(String md5) {
333 | if (isNullOrEmpty(md5)) {
334 | return false;
335 | }
336 | Pattern p = Pattern.compile("[A-Za-z0-9_]{16,40}");
337 | Matcher m = p.matcher(md5);
338 | return m.matches();
339 | }
340 |
341 | public static boolean isAllNull(Object... obj) {
342 | if (obj == null || obj.length == 0) {
343 | return true;
344 | }
345 | for (int i = 0; i < obj.length; i++) {
346 | if (!isNullOrEmpty(obj[i])) {
347 | return false;
348 | }
349 | }
350 | return true;
351 | }
352 |
353 | public static boolean isAllNull(List