提交 | 用户 | age
|
826b66
|
1 |
package com.hx.util; |
C |
2 |
|
|
3 |
import java.io.BufferedReader; |
|
4 |
import java.io.DataOutputStream; |
|
5 |
import java.io.File; |
|
6 |
import java.io.FileInputStream; |
|
7 |
import java.io.IOException; |
|
8 |
import java.io.UnsupportedEncodingException; |
|
9 |
import java.math.BigDecimal; |
|
10 |
import java.math.RoundingMode; |
|
11 |
import java.net.FileNameMap; |
|
12 |
import java.net.HttpURLConnection; |
|
13 |
import java.net.InetAddress; |
|
14 |
import java.net.URL; |
|
15 |
import java.net.URLConnection; |
|
16 |
import java.net.URLDecoder; |
|
17 |
import java.net.UnknownHostException; |
|
18 |
import java.security.MessageDigest; |
|
19 |
import java.security.NoSuchAlgorithmException; |
|
20 |
import java.sql.Timestamp; |
|
21 |
import java.text.DateFormat; |
|
22 |
import java.text.DecimalFormat; |
|
23 |
import java.text.ParseException; |
|
24 |
import java.text.SimpleDateFormat; |
|
25 |
import java.util.*; |
|
26 |
import java.util.regex.Matcher; |
|
27 |
import java.util.regex.Pattern; |
|
28 |
|
|
29 |
import javax.servlet.http.HttpServletRequest; |
|
30 |
|
|
31 |
import org.apache.commons.io.IOUtils; |
|
32 |
import org.apache.poi.hssf.usermodel.HSSFCell; |
|
33 |
import org.apache.poi.hssf.usermodel.HSSFDataFormatter; |
|
34 |
import org.apache.poi.hssf.usermodel.HSSFDateUtil; |
|
35 |
import org.apache.poi.hssf.usermodel.HSSFRow; |
|
36 |
import org.apache.poi.hssf.usermodel.HSSFSheet; |
|
37 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
|
38 |
import org.apache.poi.ss.usermodel.Cell; |
|
39 |
|
|
40 |
import net.sf.json.JSONArray; |
|
41 |
import net.sf.json.JSONException; |
|
42 |
import net.sf.json.JSONObject; |
|
43 |
import net.sourceforge.pinyin4j.PinyinHelper; |
|
44 |
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; |
|
45 |
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; |
|
46 |
import org.apache.poi.ss.usermodel.CellType; |
|
47 |
import org.springframework.cglib.beans.BeanMap; |
|
48 |
|
|
49 |
public class SimpleTool { |
|
50 |
|
|
51 |
public static SimpleDateFormat myformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
52 |
public static DecimalFormat fmt = new DecimalFormat("0.00"); |
|
53 |
public static Byte[] lock = new Byte[] {0}; |
|
54 |
|
|
55 |
/**对象转map*/ |
|
56 |
public static Map<?, ?> objectToMap(Object obj) { |
|
57 |
if(obj == null) |
|
58 |
return null; |
|
59 |
|
|
60 |
return new org.apache.commons.beanutils.BeanMap(obj); |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* 将对象装换为map |
|
65 |
* |
|
66 |
* @param bean |
|
67 |
* @return |
|
68 |
*/ |
|
69 |
public static <T> Map<String, Object> beanToMap(T bean) { |
|
70 |
Map<String, Object> map = new HashMap<>(); |
|
71 |
if (bean != null) { |
|
72 |
BeanMap beanMap = BeanMap.create(bean); |
|
73 |
for (Object key : beanMap.keySet()) { |
|
74 |
map.put(key + "", beanMap.get(key)); |
|
75 |
} |
|
76 |
} |
|
77 |
return map; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* 将对象装换为map |
|
82 |
* |
|
83 |
* @param bean |
|
84 |
* @return |
|
85 |
*/ |
|
86 |
public static Map<String, String> beanToMapS(Object bean) { |
|
87 |
Map<String, String> map = new HashMap<>(); |
|
88 |
if (bean != null) { |
|
89 |
BeanMap beanMap = BeanMap.create(bean); |
|
90 |
for (Object key : beanMap.keySet()) { |
|
91 |
if(beanMap.get(key) != null){ |
|
92 |
map.put(key + "", beanMap.get(key).toString()); |
|
93 |
}else{ |
|
94 |
map.put(key + "", ""); |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
return map; |
|
99 |
} |
|
100 |
|
|
101 |
|
|
102 |
/** |
|
103 |
* 后台格式构建返回值格式列表-后台获取列表 |
|
104 |
* @param count 返回总条数 |
|
105 |
* @return JSONObject 特定格式的JSONObject |
|
106 |
*/ |
|
107 |
public static JSONObject resAdminLsit(Integer status,String errMsg,JSONArray arr,Integer count){ |
|
108 |
|
|
109 |
JSONObject fObj = new JSONObject(); |
|
110 |
try{ |
|
111 |
fObj.put("code", status); |
|
112 |
fObj.put("count", count); |
|
113 |
fObj.put("msg", errMsg); |
|
114 |
fObj.put("data", arr); |
|
115 |
}catch (Exception e) { |
|
116 |
e.printStackTrace(); |
|
117 |
} |
|
118 |
return fObj; |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* 获取指定格式的Double 类型数据 |
|
123 |
* |
|
124 |
* @param type |
|
125 |
* 转换格式的类型 默认为 "#.#" |
|
126 |
* @param e |
|
127 |
* 要转换的数据源 |
|
128 |
**/ |
|
129 |
public static Double getTypeDouble(String type, Double e) { |
|
130 |
if (!checkNotNull(type)) |
|
131 |
type = "0.00"; |
|
132 |
fmt = new DecimalFormat(type); |
|
133 |
fmt.setRoundingMode(RoundingMode.HALF_UP); // 保留小数点后两位并四舍五入,确保价钱准确 |
|
134 |
Double d = Double.valueOf(fmt.format(e)); |
|
135 |
return d; |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* 首字母大写 |
|
140 |
* |
|
141 |
* @param str |
|
142 |
*/ |
|
143 |
public static String capitalized(String str) { |
|
144 |
if ("".equals(str.trim()) || str == null) { |
|
145 |
return ""; |
|
146 |
} |
|
147 |
String key = str.substring(0, 1).toUpperCase(); |
|
148 |
String keys = key + str.substring(1, str.length()); |
|
149 |
return keys; |
|
150 |
} |
|
151 |
|
|
152 |
/** 判断参数是否为null,如果null抛出对应异常 */ |
|
153 |
public static void checkErrMsg(Object obj, String errMsg) { |
|
154 |
if (!checkNotNull(obj)) |
|
155 |
throw new RuntimeException(errMsg); |
|
156 |
} |
|
157 |
|
|
158 |
/*** 创建文件(自动生成文件夹。)(需要真实路径) **/ |
|
159 |
public static File createFile(String mkdirPath, String fileName) throws IOException { |
|
160 |
System.out.println(mkdirPath + fileName); |
|
161 |
// mkdirPath = |
|
162 |
// "E:\\work\\soft\\MyEclipse\\apache-tomcat-7.0.61\\webapps\\ROOT\\"; |
|
163 |
|
|
164 |
File f = new File(mkdirPath); |
|
165 |
if (!f.exists()) { |
|
166 |
f.mkdirs(); |
|
167 |
} |
|
168 |
File file = new File(f, fileName); |
|
169 |
if (!file.exists()) { |
|
170 |
file.createNewFile(); |
|
171 |
} |
|
172 |
|
|
173 |
// System.out.println(file.getAbsolutePath()); |
|
174 |
return file; |
|
175 |
} |
|
176 |
|
|
177 |
/** |
|
178 |
* 构建返回值格式 |
|
179 |
* |
|
180 |
* @param status |
|
181 |
* 返回状态 |
|
182 |
* @param errMsg |
|
183 |
* 返回附带信息 |
|
184 |
* @param inf |
|
185 |
* 返回实体值 |
|
186 |
* @return JSONObject 特定格式的JSONObject |
|
187 |
*/ |
|
188 |
public static JSONObject res(Integer status, String errMsg, JSONObject inf) { |
|
189 |
JSONObject res = new JSONObject(); |
|
190 |
JSONObject fObj = new JSONObject(); |
|
191 |
try { |
|
192 |
res.put("status", status); |
|
193 |
res.put("errMsg", errMsg); |
|
194 |
|
|
195 |
fObj.put("res", res); |
|
196 |
fObj.put("inf", inf); |
|
197 |
} catch (Exception e) { |
|
198 |
e.printStackTrace(); |
|
199 |
} |
|
200 |
return fObj; |
|
201 |
} |
|
202 |
|
|
203 |
/** |
|
204 |
* Date类型转换为10位时间戳 |
|
205 |
* |
|
206 |
* @param time |
|
207 |
* @return |
|
208 |
*/ |
|
209 |
public static Integer DateToTimestamp(Date time) { |
|
210 |
Timestamp ts = new Timestamp(time.getTime()); |
|
211 |
return (int) ((ts.getTime()) / 1000); |
|
212 |
} |
|
213 |
|
|
214 |
/** 判断当前时间是什么时候 **/ |
|
215 |
public static Boolean IsTime(Date date, Integer year, Integer month, Integer day, Integer hour, Integer minute) { |
|
216 |
Calendar cal = new GregorianCalendar(); |
|
217 |
cal.setTime(date); |
|
218 |
if (year != null) { |
|
219 |
int iyear = cal.get(Calendar.YEAR); |
|
220 |
if (iyear != year) |
|
221 |
return false; |
|
222 |
} |
|
223 |
|
|
224 |
if (month != null) { |
|
225 |
int imonth = cal.get(Calendar.MONTH);// 获取月份(月份从0开始计算的) |
|
226 |
imonth = imonth + 1; |
|
227 |
if (imonth != month) |
|
228 |
return false; |
|
229 |
} |
|
230 |
|
|
231 |
if (day != null) { |
|
232 |
int iday = cal.get(Calendar.DATE);// 获取日 |
|
233 |
if (iday != day) |
|
234 |
return false; |
|
235 |
} |
|
236 |
|
|
237 |
if (hour != null) { |
|
238 |
int ihour = cal.get(Calendar.HOUR_OF_DAY);// 24小时 0- 23 |
|
239 |
if (ihour != hour) |
|
240 |
return false; |
|
241 |
} |
|
242 |
|
|
243 |
if (minute != null) { |
|
244 |
int iminute = cal.get(Calendar.MINUTE); |
|
245 |
if (iminute != minute) |
|
246 |
return false; |
|
247 |
} |
|
248 |
// Boolean b = yearTag && monthTag && dayTag && hourTag && minuteTag; |
|
249 |
return true; |
|
250 |
} |
|
251 |
|
|
252 |
/** |
|
253 |
* 根据具体时间属性判断是否相同 |
|
254 |
* |
|
255 |
*/ |
|
256 |
public static Boolean DateEquals(Date date1, Date date2, Boolean year, Boolean month, Boolean day, Boolean hour, |
|
257 |
Boolean minute) { |
|
258 |
|
|
259 |
Calendar calendar1 = new GregorianCalendar(); |
|
260 |
Calendar calendar2 = new GregorianCalendar(); |
|
261 |
calendar1.setTime(date1); |
|
262 |
calendar2.setTime(date2); |
|
263 |
|
|
264 |
if (year) { |
|
265 |
if (calendar1.get(Calendar.YEAR) != calendar2.get(Calendar.YEAR)) { |
|
266 |
return false; |
|
267 |
} |
|
268 |
} |
|
269 |
if (month) { |
|
270 |
if (calendar1.get(Calendar.MONTH) != calendar2.get(Calendar.MONTH)) { |
|
271 |
return false; |
|
272 |
} |
|
273 |
} |
|
274 |
if (day) { |
|
275 |
if (calendar1.get(Calendar.DATE) != calendar2.get(Calendar.DATE)) { |
|
276 |
return false; |
|
277 |
} |
|
278 |
} |
|
279 |
if (year) { |
|
280 |
if (calendar1.get(Calendar.HOUR_OF_DAY) != calendar2.get(Calendar.HOUR_OF_DAY)) { |
|
281 |
return false; |
|
282 |
} |
|
283 |
} |
|
284 |
if (year) { |
|
285 |
if (calendar1.get(Calendar.MINUTE) != calendar2.get(Calendar.MINUTE)) { |
|
286 |
return false; |
|
287 |
} |
|
288 |
} |
|
289 |
|
|
290 |
// Boolean b = yearTag && monthTag && dayTag && hourTag && minuteTag; |
|
291 |
|
|
292 |
return true; |
|
293 |
} |
|
294 |
|
|
295 |
/** 获取10位数时间戳 */ |
|
296 |
public static Integer getTenTime(Date date) { |
|
297 |
Timestamp ts = new Timestamp(date.getTime()); |
|
298 |
return (int) ((ts.getTime()) / 1000); |
|
299 |
} |
|
300 |
|
|
301 |
/** |
|
302 |
* 获取指定日期的明天 difDay 把日期往后增加一天.整数往后推,负数往前移动 |
|
303 |
* |
|
304 |
* @return date |
|
305 |
*/ |
|
306 |
@SuppressWarnings("static-access") |
|
307 |
public static Date getDetailDay(Date today, int difDay) { |
|
308 |
Calendar calendar = new GregorianCalendar(); |
|
309 |
calendar.setTime(today); |
|
310 |
calendar.add(calendar.DATE, difDay);// 把日期往后增加一天.整数往后推,负数往前移动 |
|
311 |
Date d = calendar.getTime(); |
|
312 |
return d; |
|
313 |
} |
|
314 |
|
|
315 |
/** |
|
316 |
* 获取想要的格式的时间 |
|
317 |
* |
|
318 |
* @return String |
|
319 |
*/ |
|
320 |
public static String getTypeDate(String type, Date date) { |
|
321 |
if (date == null) |
|
322 |
return null; |
|
323 |
|
|
324 |
if (SimpleTool.checkNotNull(type)) { |
|
325 |
myformat = new SimpleDateFormat(type); |
|
326 |
String returnDate = myformat.format(date); |
|
327 |
myformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
328 |
return returnDate; |
|
329 |
} else { |
|
330 |
String returnDate = myformat.format(date); |
|
331 |
return returnDate; |
|
332 |
} |
|
333 |
|
|
334 |
} |
|
335 |
|
|
336 |
/** |
|
337 |
* 比较两个时间天数差 startTime - endTime |
|
338 |
* |
|
339 |
* @param type |
|
340 |
* (无效) |
|
341 |
* @return String |
|
342 |
* @throws ParseException |
|
343 |
*/ |
|
344 |
public static int getDiffDate(String type, Date startTime, Date endTime) throws ParseException { |
|
345 |
type = "yyyyMMdd"; |
|
346 |
SimpleDateFormat myformat = new SimpleDateFormat(type); |
|
347 |
int i = 0; |
|
348 |
i = (int) ((myformat.parse(myformat.format(startTime)).getTime() |
|
349 |
- myformat.parse(myformat.format(endTime)).getTime()) / (1000 * 60 * 60 * 24)); |
|
350 |
// return |
|
351 |
// myformat.parse(myformat.format(date1)).compareTo(myformat.parse(myformat.format(date2))); |
|
352 |
return i; |
|
353 |
} |
|
354 |
|
|
355 |
/** |
|
356 |
* 得到两个时间的分钟差 date1 - date2 这个是个奇葩。但是整型了。 |
|
357 |
* |
|
358 |
* @return int |
|
359 |
* @throws ParseException |
|
360 |
*/ |
|
361 |
public static int getLeftDateForMinue(Date date1, Date date2) throws ParseException { |
|
362 |
long times = date1.getTime() - date2.getTime(); |
|
363 |
times = times / (1000 * 60); |
|
364 |
return (int) times; |
|
365 |
} |
|
366 |
|
|
367 |
/**把字符串转为特定时间格式 |
|
368 |
* |
|
369 |
* @param type 时间格式 |
|
370 |
* @param sDate 字符串时间(可以为空) |
|
371 |
* @return date |
|
372 |
* @throws ParseException |
|
373 |
*/ |
|
374 |
public static Date parseStringForDate(String type, String sDate){ |
|
375 |
Date date = null; |
|
376 |
try { |
|
377 |
if(SimpleTool.checkNotNull(sDate)) { |
|
378 |
SimpleDateFormat myformat = new SimpleDateFormat(type); |
|
379 |
date = myformat.parse(sDate); |
|
380 |
} |
|
381 |
} catch (Exception e) { |
|
382 |
e.printStackTrace(); |
|
383 |
} |
|
384 |
return date; |
|
385 |
} |
|
386 |
|
|
387 |
/** |
|
388 |
* 判断是否为空 |
|
389 |
*/ |
|
390 |
public static boolean checkNotNull(Object o) { |
|
391 |
boolean b = false; |
|
392 |
if (o != null && !"".equals(o)&&!"undefined".equals(o)&&!" ".equals(o)) { |
|
393 |
b = true; |
|
394 |
} |
|
395 |
return b; |
|
396 |
} |
|
397 |
|
|
398 |
/** |
|
399 |
* 判断是否为空 (多个) |
|
400 |
*/ |
|
401 |
public static boolean checkNotNullM(Object[] os) { |
|
402 |
boolean b = true; |
|
403 |
for (Object o : os) { |
|
404 |
if (o == null || "".equals(o)) |
|
405 |
return false; |
|
406 |
} |
|
407 |
return b; |
|
408 |
} |
|
409 |
|
|
410 |
/** |
|
411 |
* 获取服务器端的webapps路径 |
|
412 |
* |
|
413 |
* @return |
|
414 |
*/ |
|
415 |
public String findServerPath() { |
|
416 |
String classPath = this.getClass().getClassLoader().getResource("/").getPath(); |
|
417 |
try { |
|
418 |
classPath = URLDecoder.decode(classPath, "gb2312"); |
|
419 |
} catch (UnsupportedEncodingException e) { |
|
420 |
e.printStackTrace(); |
|
421 |
} |
|
422 |
String[] strPath = classPath.split("/"); |
|
423 |
String path = ""; |
|
424 |
for (int i = 0; i < strPath.length; i++) { |
|
425 |
if (i > 0 && i <= 3) { |
|
426 |
path = path + strPath[i] + "/"; |
|
427 |
} |
|
428 |
} |
|
429 |
return path; |
|
430 |
} |
|
431 |
|
|
432 |
/** |
|
433 |
* 删除文件 输入真是路径 |
|
434 |
*/ |
|
435 |
public static void deleteFile(String sPath) { |
|
436 |
File file = new File(sPath); |
|
437 |
// 判断目录或文件是否存在 |
|
438 |
if (file.exists()) { |
|
439 |
// 判断是否为文件 |
|
440 |
if (file.isFile()) { // 为文件时调用删除文件方法 |
|
441 |
deleteFileForOne(sPath); |
|
442 |
} else { // 为目录时调用删除目录方法 |
|
443 |
throw new RuntimeException("dir is no del"); |
|
444 |
// deleteDirectory(sPath); |
|
445 |
} |
|
446 |
} |
|
447 |
} |
|
448 |
|
|
449 |
/** |
|
450 |
* 删除目录(文件夹)以及目录下的文件 |
|
451 |
* |
|
452 |
* @param sPath |
|
453 |
* 被删除目录的文件路径 |
|
454 |
* @return 目录删除成功返回true,否则返回false |
|
455 |
*/ |
|
456 |
public static boolean deleteDirectory(String sPath) { |
|
457 |
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符 |
|
458 |
if (!sPath.endsWith(File.separator)) { |
|
459 |
sPath = sPath + File.separator; |
|
460 |
} |
|
461 |
File dirFile = new File(sPath); |
|
462 |
// 如果dir对应的文件不存在,或者不是一个目录,则退出 |
|
463 |
if (!dirFile.exists() || !dirFile.isDirectory()) { |
|
464 |
return false; |
|
465 |
} |
|
466 |
Boolean flag = true; |
|
467 |
// 删除文件夹下的所有文件(包括子目录) |
|
468 |
File[] files = dirFile.listFiles(); |
|
469 |
for (int i = 0; i < files.length; i++) { |
|
470 |
// 删除子文件 |
|
471 |
if (files[i].isFile()) { |
|
472 |
flag = deleteFileForOne(files[i].getAbsolutePath()); |
|
473 |
if (!flag) |
|
474 |
break; |
|
475 |
} // 删除子目录 |
|
476 |
else { |
|
477 |
flag = deleteDirectory(files[i].getAbsolutePath()); |
|
478 |
if (!flag) |
|
479 |
break; |
|
480 |
} |
|
481 |
} |
|
482 |
if (!flag) |
|
483 |
return false; |
|
484 |
// 删除当前目录 |
|
485 |
if (dirFile.delete()) { |
|
486 |
return true; |
|
487 |
} else { |
|
488 |
return false; |
|
489 |
} |
|
490 |
} |
|
491 |
|
|
492 |
/** |
|
493 |
* 删除单个文件 |
|
494 |
* |
|
495 |
* @param sPath |
|
496 |
* 被删除文件的文件名 |
|
497 |
* @return 单个文件删除成功返回true,否则返回false |
|
498 |
*/ |
|
499 |
public static boolean deleteFileForOne(String sPath) { |
|
500 |
Boolean flag = false; |
|
501 |
File file = new File(sPath); |
|
502 |
// 路径为文件且不为空则进行删除 |
|
503 |
if (file.isFile() && file.exists()) { |
|
504 |
file.delete(); |
|
505 |
flag = true; |
|
506 |
} |
|
507 |
return flag; |
|
508 |
} |
|
509 |
|
|
510 |
|
|
511 |
/** |
|
512 |
* 获取uuid |
|
513 |
*/ |
|
514 |
public static String getUUIDName() { |
|
515 |
return UUID.randomUUID().toString(); |
|
516 |
} |
|
517 |
|
|
518 |
|
|
519 |
/** 获取异常信息 **/ |
|
520 |
public static String getExceptionMsg(Exception e) { |
|
521 |
StringBuffer emsg = new StringBuffer(); |
|
522 |
if (e != null) { |
|
523 |
StackTraceElement[] st = e.getStackTrace(); |
|
524 |
for (StackTraceElement stackTraceElement : st) { |
|
525 |
String exclass = stackTraceElement.getClassName(); |
|
526 |
String method = stackTraceElement.getMethodName(); |
|
527 |
emsg.append("[类:" + exclass + "]调用---" + method + "----时在第" + stackTraceElement.getLineNumber() |
|
528 |
+ "行代码处发生异常!异常类型:" + e.toString() + "(" + e.getMessage() + ")\r\n"); |
|
529 |
} |
|
530 |
} |
|
531 |
return emsg.toString(); |
|
532 |
} |
|
533 |
|
|
534 |
/** |
|
535 |
* 获取指定格式的Double 类型数据 |
|
536 |
* |
|
537 |
* 转换格式的类型 默认为 "#.##" |
|
538 |
* @param e |
|
539 |
* 要转换的数据源 |
|
540 |
**/ |
|
541 |
public static Double getTypeDouble(Double e) { |
|
542 |
fmt.setRoundingMode(RoundingMode.HALF_UP); // 保留小数点后两位并四舍五入,确保价钱准确 |
|
543 |
Double d = Double.valueOf(fmt.format(e)); |
|
544 |
return d; |
|
545 |
} |
|
546 |
|
|
547 |
|
|
548 |
/** |
|
549 |
* SHA1 加密 |
|
550 |
* |
|
551 |
* @param decript |
|
552 |
* @return |
|
553 |
*/ |
|
554 |
public static String SHA1(String decript) { |
|
555 |
try { |
|
556 |
MessageDigest digest = MessageDigest.getInstance("SHA-1"); |
|
557 |
digest.update(decript.getBytes()); |
|
558 |
byte messageDigest[] = digest.digest(); |
|
559 |
// Create Hex String |
|
560 |
StringBuffer hexString = new StringBuffer(); |
|
561 |
// 字节数组转换为 十六进制 数 |
|
562 |
for (int i = 0; i < messageDigest.length; i++) { |
|
563 |
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); |
|
564 |
if (shaHex.length() < 2) { |
|
565 |
hexString.append(0); |
|
566 |
} |
|
567 |
hexString.append(shaHex); |
|
568 |
} |
|
569 |
return hexString.toString(); |
|
570 |
} catch (NoSuchAlgorithmException e) { |
|
571 |
e.printStackTrace(); |
|
572 |
} |
|
573 |
return ""; |
|
574 |
} |
|
575 |
|
|
576 |
|
|
577 |
public static String getLocalIP() { |
|
578 |
InetAddress addr = null; |
|
579 |
try { |
|
580 |
addr = InetAddress.getLocalHost(); |
|
581 |
} catch (UnknownHostException e) { |
|
582 |
e.printStackTrace(); |
|
583 |
} |
|
584 |
|
|
585 |
byte[] ipAddr = addr.getAddress(); |
|
586 |
String ipAddrStr = ""; |
|
587 |
for (int i = 0; i < ipAddr.length; i++) { |
|
588 |
if (i > 0) { |
|
589 |
ipAddrStr += "."; |
|
590 |
} |
|
591 |
ipAddrStr += ipAddr[i] & 0xFF; |
|
592 |
} |
|
593 |
// System.out.println("i am ip:......"+ipAddrStr); |
|
594 |
return ipAddrStr; |
|
595 |
} |
|
596 |
|
|
597 |
/**随机生成字符串(0-9) |
|
598 |
* |
|
599 |
* @param lengthCount 长度 |
|
600 |
* @return |
|
601 |
*/ |
|
602 |
public static String generateCardNo(Integer lengthCount) { |
|
603 |
if(!SimpleTool.checkNotNull(lengthCount)) { |
|
604 |
lengthCount = 6; |
|
605 |
} |
|
606 |
java.util.Random r=new java.util.Random(); |
|
607 |
StringBuilder str = new StringBuilder();//定义变长字符串 |
|
608 |
for(int i=0;i<lengthCount;i++){ |
|
609 |
str.append(r.nextInt(10)); |
|
610 |
} |
|
611 |
return str.toString(); |
|
612 |
} |
|
613 |
|
|
614 |
/**判断是不是视频文件 |
|
615 |
* @param fileName 文件名称 |
|
616 |
* @return boolean true是视频文件 |
|
617 |
*/ |
|
618 |
public static boolean getMimeType(String fileName) { |
|
619 |
boolean b = false; |
|
620 |
FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
|
621 |
String type = fileNameMap.getContentTypeFor(fileName); |
|
622 |
//是视频type是为空的 |
|
623 |
if(!checkNotNull(type)) { |
|
624 |
b = true; |
|
625 |
} |
|
626 |
return b; |
|
627 |
} |
|
628 |
|
|
629 |
/** |
|
630 |
* 判断是否为视频 |
|
631 |
* @param fileName |
|
632 |
* @return |
|
633 |
*/ |
|
634 |
public static String isVideo(String fileName) { |
|
635 |
FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
|
636 |
String type = fileNameMap.getContentTypeFor(fileName); |
|
637 |
return type; |
|
638 |
} |
|
639 |
|
|
640 |
/**判断是否是中文*/ |
|
641 |
public static boolean isChineseChar(String str){ |
|
642 |
boolean temp = false; |
|
643 |
Pattern p=Pattern.compile("[\u4e00-\u9fa5]"); |
|
644 |
Matcher m=p.matcher(str); |
|
645 |
if(m.find()){ |
|
646 |
temp = true; |
|
647 |
} |
|
648 |
return temp; |
|
649 |
} |
|
650 |
|
|
651 |
/**去除字符串中不是中文的字符 |
|
652 |
* |
|
653 |
*/ |
|
654 |
public static String deleteNoChinese(String str){ |
|
655 |
String name=""; |
|
656 |
if(checkNotNull(str)){ |
|
657 |
for(int i=0;i<str.length();i++){ |
|
658 |
String s = str.substring(i, i+1); |
|
659 |
if(isChineseChar(s)){ |
|
660 |
name = name +s; |
|
661 |
} |
|
662 |
} |
|
663 |
} |
|
664 |
return name; |
|
665 |
} |
|
666 |
|
|
667 |
/**生成字母(全部是中文的)*/ |
|
668 |
public static String convertHanzi3Pinyin(String hanzi,boolean full){ |
|
669 |
hanzi = deleteNoChinese(hanzi); |
|
670 |
hanzi = convertHanzi2Pinyin(hanzi,false); |
|
671 |
return hanzi; |
|
672 |
} |
|
673 |
|
|
674 |
/***获取字符串拼音的第一个字母(大写)**/ |
|
675 |
public static String convertStringPinyinOne(String hanzi) { |
|
676 |
if(SimpleTool.checkNotNull(hanzi)) { |
|
677 |
//转化为便宜 |
|
678 |
hanzi = convertHanzi2Pinyin(hanzi,false); |
|
679 |
//转化大写 |
|
680 |
hanzi = hanzi.toUpperCase(); |
|
681 |
/*//获取第一个 |
|
682 |
byte[] datas = hanzi.getBytes(); |
|
683 |
if(datas.length>0) { |
|
684 |
byte[] datas2 = new byte[] {datas[0]}; |
|
685 |
hanzi = new String(datas2); |
|
686 |
}else { |
|
687 |
hanzi = ""; |
|
688 |
}*/ |
|
689 |
} |
|
690 |
return hanzi; |
|
691 |
} |
|
692 |
|
|
693 |
|
|
694 |
/***将汉字转成拼音(取首字母或全拼) |
|
695 |
* @param hanzi |
|
696 |
* @param full 是否全拼 |
|
697 |
* @return |
|
698 |
*/ |
|
699 |
public static String convertHanzi2Pinyin(String hanzi,boolean full){ |
|
700 |
//获取第一个字 |
|
701 |
/* if(checkNotNull(hanzi)){ |
|
702 |
boolean isHave = true; |
|
703 |
Integer l = hanzi.length(); |
|
704 |
Integer index = 0; |
|
705 |
while (isHave) { |
|
706 |
String hanzi2 = hanzi.substring(index,index+1); |
|
707 |
if(isChineseChar(hanzi2)){ |
|
708 |
isHave = false; |
|
709 |
hanzi = hanzi2; |
|
710 |
}else{ |
|
711 |
if(index<l-1){ |
|
712 |
index++; |
|
713 |
}else{ |
|
714 |
isHave = false; |
|
715 |
} |
|
716 |
} |
|
717 |
} |
|
718 |
} |
|
719 |
*/ |
|
720 |
//去除所有的空格 |
|
721 |
if(checkNotNull(hanzi)){ |
|
722 |
hanzi = hanzi.replaceAll(" ", ""); |
|
723 |
} |
|
724 |
|
|
725 |
/*** |
|
726 |
* ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言 |
|
727 |
* ^[\u4E00-\u9FFF]+$ 匹配简体和繁体 |
|
728 |
* ^[\u4E00-\u9FA5]+$ 匹配简体 |
|
729 |
*/ |
|
730 |
String regExp="^[\u4E00-\u9FFF]+$"; |
|
731 |
StringBuffer sb=new StringBuffer(); |
|
732 |
if(hanzi==null||"".equals(hanzi.trim())){ |
|
733 |
return ""; |
|
734 |
} |
|
735 |
String pinyin=""; |
|
736 |
for(int i=0;i<hanzi.length();i++){ |
|
737 |
char unit=hanzi.charAt(i); |
|
738 |
if(match(String.valueOf(unit),regExp)){//是汉字,则转拼音 |
|
739 |
pinyin=convertSingleHanzi2Pinyin(unit); |
|
740 |
if(full){ |
|
741 |
sb.append(pinyin); |
|
742 |
}else{ |
|
743 |
sb.append(pinyin.charAt(0)); |
|
744 |
} |
|
745 |
}else{ |
|
746 |
sb.append(unit); |
|
747 |
} |
|
748 |
} |
|
749 |
return sb.toString(); |
|
750 |
} |
|
751 |
|
|
752 |
/***将单个汉字转成拼音 |
|
753 |
* @param hanzi |
|
754 |
* @return |
|
755 |
*/ |
|
756 |
private static String convertSingleHanzi2Pinyin(char hanzi){ |
|
757 |
HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat(); |
|
758 |
outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); |
|
759 |
String[] res; |
|
760 |
StringBuffer sb=new StringBuffer(); |
|
761 |
try { |
|
762 |
res = PinyinHelper.toHanyuPinyinStringArray(hanzi,outputFormat); |
|
763 |
sb.append(res[0]);//对于多音字,只用第一个拼音 |
|
764 |
} catch (Exception e) { |
|
765 |
e.printStackTrace(); |
|
766 |
return ""; |
|
767 |
} |
|
768 |
return sb.toString(); |
|
769 |
} |
|
770 |
|
|
771 |
/***@param str 源字符串 |
|
772 |
* @param regex 正则表达式 |
|
773 |
* @return 是否匹配 |
|
774 |
*/ |
|
775 |
public static boolean match(String str,String regex){ |
|
776 |
Pattern pattern=Pattern.compile(regex); |
|
777 |
Matcher matcher=pattern.matcher(str); |
|
778 |
return matcher.find(); |
|
779 |
} |
|
780 |
|
|
781 |
/** 读取 Excel文件内容(3.24 quan) |
|
782 |
* |
|
783 |
* @param excel_name File |
|
784 |
* @return |
|
785 |
* @throws Exception |
|
786 |
*/ |
|
787 |
public static List<String[]> readExcelByeFile2(File excel_name) throws Exception { |
|
788 |
// 结果集 |
|
789 |
List<String[]> list = new ArrayList<String[]>(); |
|
790 |
HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream(excel_name)); |
|
791 |
|
|
792 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
|
793 |
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0); |
|
794 |
|
|
795 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 |
|
796 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
797 |
HSSFRow hssfrow = hssfsheet.getRow(j); |
|
798 |
if(hssfrow!=null){ |
|
799 |
int col = hssfrow.getPhysicalNumberOfCells(); |
|
800 |
// 单行数据 |
|
801 |
String[] arrayString = new String[col]; |
|
802 |
for (int i = 0; i < col; i++) { |
|
803 |
HSSFCell cell = hssfrow.getCell(i); |
|
804 |
if (cell == null) { |
|
805 |
arrayString[i] = ""; |
|
806 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
|
807 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
|
808 |
short format = cell.getCellStyle().getDataFormat(); |
|
809 |
SimpleDateFormat sdf = null; |
|
810 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
811 |
//日期 |
|
812 |
sdf = new SimpleDateFormat("yyyy-MM-dd"); |
|
813 |
}else if (format == 20 || format == 32) { |
|
814 |
//时间 |
|
815 |
sdf = new SimpleDateFormat("HH:mm"); |
|
816 |
}else if (CellType.NUMERIC == cell.getCellType()) { |
|
817 |
if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
818 |
Date d = cell.getDateCellValue(); |
|
819 |
DateFormat formater = new SimpleDateFormat("yyyy年"); |
|
820 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
821 |
arrayString[i] = formater.format(d); |
|
822 |
} else { |
|
823 |
arrayString[i] = new BigDecimal(cell.getNumericCellValue()).toString(); |
|
824 |
} |
|
825 |
} |
|
826 |
} else {// 如果EXCEL表格中的数据类型为字符串型 |
|
827 |
arrayString[i] = cell.getStringCellValue().trim(); |
|
828 |
} |
|
829 |
} |
|
830 |
list.add(arrayString); |
|
831 |
} |
|
832 |
} |
|
833 |
return list; |
|
834 |
} |
|
835 |
|
|
836 |
/** |
|
837 |
* 读取 Excel文件内容 |
|
838 |
* |
|
839 |
* @param excel_name |
|
840 |
* @param header 是否包括表头 |
|
841 |
* @return |
|
842 |
* @throws Exception |
|
843 |
*/ |
|
844 |
public static List<String[]> readExcelByeFileData(File excel_name,boolean header) throws Exception { |
|
845 |
// 结果集 |
|
846 |
List<String[]> list = new ArrayList<String[]>(); |
|
847 |
|
|
848 |
HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream( |
|
849 |
excel_name)); |
|
850 |
|
|
851 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
|
852 |
for(int s=0;s<hssfworkbook.getNumberOfSheets();s++) { |
|
853 |
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(s); |
|
854 |
int col = 0; |
|
855 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
856 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
857 |
HSSFRow hssfrow = hssfsheet.getRow(j); |
|
858 |
if(hssfrow!=null){ |
|
859 |
if(j == 0) { |
|
860 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
861 |
if(!header) { |
|
862 |
//不包括表头 |
|
863 |
continue; |
|
864 |
} |
|
865 |
} |
|
866 |
// 单行数据 |
|
867 |
String[] arrayString = new String[col]; |
|
868 |
for (int i = 0; i < col; i++) { |
|
869 |
HSSFCell cell = hssfrow.getCell(i); |
|
870 |
if (cell == null) { |
|
871 |
arrayString[i] = ""; |
|
872 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
|
873 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
|
874 |
if (CellType.NUMERIC == cell.getCellType()) { |
|
875 |
short format = cell.getCellStyle().getDataFormat(); |
|
876 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
877 |
//日期(中文时间格式的) |
|
878 |
Date d = cell.getDateCellValue(); |
|
879 |
DateFormat formater = new SimpleDateFormat("yyyy年"); |
|
880 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
881 |
arrayString[i] = formater.format(d); |
|
882 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
883 |
Date d = cell.getDateCellValue(); |
|
884 |
DateFormat formater = new SimpleDateFormat("yyyy年"); |
|
885 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
886 |
arrayString[i] = formater.format(d); |
|
887 |
} else { |
|
888 |
if(CellType.STRING == cell.getCellType()){ |
|
889 |
arrayString[i] =cell.getStringCellValue(); |
|
890 |
}else if(CellType.FORMULA==cell.getCellType()){ |
|
891 |
arrayString[i] =cell.getCellFormula(); |
|
892 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
|
893 |
HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); |
|
894 |
arrayString[i] =dataFormatter.formatCellValue(cell); |
|
895 |
} |
|
896 |
} |
|
897 |
} |
|
898 |
} else if(cell.getCellType() == CellType.BLANK){ |
|
899 |
arrayString[i] = ""; |
|
900 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
901 |
arrayString[i] = cell.getStringCellValue().trim(); |
|
902 |
} |
|
903 |
} |
|
904 |
list.add(arrayString); |
|
905 |
} |
|
906 |
} |
|
907 |
} |
|
908 |
return list; |
|
909 |
} |
|
910 |
|
|
911 |
/** |
|
912 |
* 读取 Excel文件内容(学校信息专用) |
|
913 |
* |
|
914 |
* @param excel_name |
|
915 |
* @return |
|
916 |
* @throws Exception |
|
917 |
*/ |
|
918 |
public static List<String[]> readExcelByeFile(File excel_name) throws Exception { |
|
919 |
// 结果集 |
|
920 |
List<String[]> list = new ArrayList<String[]>(); |
|
921 |
|
|
922 |
HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream( |
|
923 |
excel_name)); |
|
924 |
|
|
925 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
|
926 |
for(int s=0;s<hssfworkbook.getNumberOfSheets();s++) { |
|
927 |
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(s); |
|
928 |
int col = 0; |
|
929 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
930 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
931 |
HSSFRow hssfrow = hssfsheet.getRow(j); |
|
932 |
if(hssfrow!=null){ |
|
933 |
if(j == 0) { |
|
934 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
935 |
}else { |
|
936 |
// int col = hssfrow.getPhysicalNumberOfCells(); |
|
937 |
// 单行数据 |
|
938 |
String[] arrayString = new String[col]; |
|
939 |
for (int i = 0; i < col; i++) { |
|
940 |
HSSFCell cell = hssfrow.getCell(i); |
|
941 |
if (cell == null) { |
|
942 |
arrayString[i] = ""; |
|
943 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
|
944 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
|
945 |
if (CellType.NUMERIC == cell.getCellType()) { |
|
946 |
short format = cell.getCellStyle().getDataFormat(); |
|
947 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
948 |
//日期(中文时间格式的) |
|
949 |
Date d = cell.getDateCellValue(); |
|
950 |
DateFormat formater = new SimpleDateFormat("yyyy年"); |
|
951 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
952 |
arrayString[i] = formater.format(d); |
|
953 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
954 |
Date d = cell.getDateCellValue(); |
|
955 |
DateFormat formater = new SimpleDateFormat("yyyy年"); |
|
956 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
957 |
arrayString[i] = formater.format(d); |
|
958 |
} else { |
|
959 |
if(CellType.STRING == cell.getCellType()){ |
|
960 |
arrayString[i] =cell.getStringCellValue(); |
|
961 |
}else if(CellType.FORMULA==cell.getCellType()){ |
|
962 |
arrayString[i] =cell.getCellFormula(); |
|
963 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
|
964 |
HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); |
|
965 |
arrayString[i] =dataFormatter.formatCellValue(cell); |
|
966 |
} |
|
967 |
} |
|
968 |
} |
|
969 |
} else if(cell.getCellType() == CellType.BLANK){ |
|
970 |
arrayString[i] = ""; |
|
971 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
972 |
cell.setCellType(CellType.STRING); |
|
973 |
String name = cell.getStringCellValue().trim(); |
|
974 |
if(name.equals("-")) { |
|
975 |
arrayString[i] = ""; |
|
976 |
}else { |
|
977 |
arrayString[i] = name; |
|
978 |
} |
|
979 |
} |
|
980 |
} |
|
981 |
list.add(arrayString); |
|
982 |
} |
|
983 |
} |
|
984 |
} |
|
985 |
} |
|
986 |
return list; |
|
987 |
} |
|
988 |
|
|
989 |
/**获取字符串里面的数字,按顺序排序*/ |
|
990 |
public static String stringNumber(String data) { |
|
991 |
String da = null; |
|
992 |
String regEc = "[^0-9]"; |
|
993 |
Pattern pattern = Pattern.compile(regEc); |
|
994 |
Matcher m = pattern.matcher(data); |
|
995 |
da = m.replaceAll("").trim(); |
|
996 |
return da; |
|
997 |
} |
|
998 |
|
|
999 |
/** |
|
1000 |
* @param url:请求url |
|
1001 |
* @param content: 请求体(参数) |
|
1002 |
* @return errorStr:错误信息;status:状态码,response:返回数据 |
|
1003 |
* @throws JSONException |
|
1004 |
*/ |
|
1005 |
public static JSONObject requestData(String url, String content) throws JSONException { |
|
1006 |
JSONObject obj = new JSONObject(); |
|
1007 |
String errMsg_r = ""; |
|
1008 |
String status_r = "0"; |
|
1009 |
String response = ""; |
|
1010 |
//PrintWriter out = null; |
|
1011 |
DataOutputStream out = null; |
|
1012 |
BufferedReader in = null; |
|
1013 |
try { |
|
1014 |
URL realUrl = new URL(url); |
|
1015 |
// 打开和URL之间的连接 |
|
1016 |
URLConnection conn = realUrl.openConnection(); |
|
1017 |
HttpURLConnection httpUrlConnection = (HttpURLConnection) conn; |
|
1018 |
// 设置请求属性 |
|
1019 |
httpUrlConnection.setRequestProperty("Content-type", "application/json;charset=UTF-8"); |
|
1020 |
//httpUrlConnection.setRequestProperty("Content-Type", "application/json"); |
|
1021 |
//httpUrlConnection.setRequestProperty("Charset", "UTF-8"); |
|
1022 |
httpUrlConnection.setRequestProperty("x-adviewrtb-version", "2.1"); |
|
1023 |
// 发送POST请求必须设置如下两行 |
|
1024 |
httpUrlConnection.setDoOutput(true); |
|
1025 |
httpUrlConnection.setDoInput(true); |
|
1026 |
// 获取URLConnection对象对应的输出流 |
|
1027 |
out = new DataOutputStream(httpUrlConnection.getOutputStream()); |
|
1028 |
//out = new PrintWriter(httpUrlConnection.getOutputStream()); |
|
1029 |
// 发送请求参数 |
|
1030 |
//out.write(content); |
|
1031 |
out.write(content.getBytes("UTF-8")); |
|
1032 |
// flush输出流的缓冲 |
|
1033 |
out.flush(); |
|
1034 |
httpUrlConnection.connect(); |
|
1035 |
// 定义BufferedReader输入流来读取URL的响应 |
|
1036 |
// in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream())); |
|
1037 |
String wxMsgXml = IOUtils.toString(httpUrlConnection.getInputStream(), "utf-8"); |
|
1038 |
System.out.println("wxMsgXml:"+wxMsgXml); |
|
1039 |
if(SimpleTool.checkNotNull(wxMsgXml)) { |
|
1040 |
obj = JSONObject.fromObject(wxMsgXml); |
|
1041 |
} |
|
1042 |
/* String line; |
|
1043 |
while ((line = in.readLine()) != null) { |
|
1044 |
response += line; |
|
1045 |
} */ |
|
1046 |
status_r = new Integer(httpUrlConnection.getResponseCode()).toString(); |
|
1047 |
} catch (Exception e) { |
|
1048 |
System.out.println("发送 POST 请求出现异常!" + e); |
|
1049 |
errMsg_r = e.getMessage(); |
|
1050 |
e.printStackTrace(); |
|
1051 |
} |
|
1052 |
// 使用finally块来关闭输出流、输入流 |
|
1053 |
finally { |
|
1054 |
try { |
|
1055 |
if (out != null) { out.close();} |
|
1056 |
if (in != null) {in.close();} |
|
1057 |
} catch (Exception ex) { |
|
1058 |
ex.printStackTrace(); |
|
1059 |
} |
|
1060 |
} |
|
1061 |
obj.put("errMsg_r", errMsg_r); |
|
1062 |
obj.put("status_r", status_r); |
|
1063 |
return obj; |
|
1064 |
} |
|
1065 |
|
|
1066 |
/**随机生成字数(整数)(包括 0) |
|
1067 |
* @param maxNumber 最大值 |
|
1068 |
* @return |
|
1069 |
*/ |
|
1070 |
public static Integer randomData(Integer maxNumber) { |
|
1071 |
java.util.Random r=new java.util.Random(); |
|
1072 |
return r.nextInt(maxNumber); |
|
1073 |
} |
|
1074 |
|
|
1075 |
private static final String[] HEADERS_TO_TRY = {"X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", |
|
1076 |
"HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED", "HTTP_X_CLUSTER_CLIENT_IP", "HTTP_CLIENT_IP", |
|
1077 |
"HTTP_FORWARDED_FOR", "HTTP_FORWARDED", "HTTP_VIA", "REMOTE_ADDR", "X-Real-IP"}; |
|
1078 |
|
|
1079 |
/** |
|
1080 |
* getClientIpAddress:(获取用户ip,可穿透代理). <br/> |
|
1081 |
* @param request |
|
1082 |
* @return |
|
1083 |
* @author chenjiahe |
|
1084 |
* @Date 2018年3月2日下午4:41:47 |
|
1085 |
* @since JDK 1.7 |
|
1086 |
*/ |
|
1087 |
public static String getClientIpAddress(HttpServletRequest request) { |
|
1088 |
for (String header : HEADERS_TO_TRY) { |
|
1089 |
String ip = request.getHeader(header); |
|
1090 |
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { |
|
1091 |
return ip; |
|
1092 |
} |
|
1093 |
} |
|
1094 |
return request.getRemoteAddr(); |
|
1095 |
} |
|
1096 |
|
|
1097 |
} |