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