-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathUtility.java
More file actions
549 lines (495 loc) · 15.7 KB
/
Copy pathUtility.java
File metadata and controls
549 lines (495 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package com.java110.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import sun.net.www.protocol.https.Handler;
import com.java110.common.Global;
import com.java110.controller.BaseController;
/**
* 工具类
*
* @author wuxw
* @date 2015-11-28
* version 1.0
*/
public class Utility extends BaseController{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 日志打印
*
* add by wuxw 2015-11-28
* @param name 变量名
* @param value 变量值
*/
public static void printLog(String name ,String value){
logger.debug(name+Global.LOGPLUS+":"+Global.LOGPLUS+value+Global.LOGPLUS);
}
/**
* 打印日志(说明)
*
* add by wuxw 2015-11-28
* @param str
*/
public static void printLog(String str){
logger.debug(Global.LOGPLUS+str+Global.LOGPLUS);
}
//校验是不是null或空字符串
public static Boolean isNotEmpty(String str){
if(str == null || "".equals(str)){
return false;
}
return true;
}
/**
* 根据domain 和 hcode 获取 pcode
*
* add by wuxw 2015-12-28
* @param domain
* @param pCode
*/
public static String getPCodeByDomainAndHcode(String domain,String hCode){
if (!Common.isNullOrEmpty(domain) || !Common.isNullOrEmpty(hCode)){
return null;
}
List<Map> codeMaps = StaticMapUtil.getCodeMapBeans();
for(Map codeMap : codeMaps){
if(domain.equals(codeMap.get("domain")) && hCode.equals(codeMap.get("hcode"))){
return codeMap.get("pcode").toString();
}
}
return null;
}
/**
* 根据hcode 获取 pcode
*
* add by wuxw 2015-12-28
* @param domain
* @param pCode
*/
public static String getPCodeByHcode(String hCode){
if ( Common.isNullOrEmpty(hCode)){
return null;
}
List<Map> codeMaps = StaticMapUtil.getCodeMapBeans();
for(Map codeMap : codeMaps){
if(hCode.equals(codeMap.get("hcode"))){
return codeMap.get("pcode").toString();
}
}
return null;
}
/**
* 随机红包处理(个人)
*
* add by wuxw 2016-2-17
*/
public static List<Map> randomRedPacket(int copies, double money) {
// int copies = 7; // 份数
// double money = 20.00;
double min = 0.01;
double m = 0;
List<Map> moneyList = new ArrayList<Map>();
Map<String, Double> moneyMap = null;
int copiesCount = copies;
for (int i = 0; i < copiesCount; i++) {
// 最小红包金额低于0.01元时
if (Amount.div(money, copies) < min) {
money += m;
double c_min_last_money = Amount.sub(money, (Amount.mul(min,
copies))); // 当金额不够时分前一个红包的金额,补充后面红包,以满足最小红包不能超过0.01
moneyList.remove(moneyMap);
moneyMap = new HashMap<String, Double>();
moneyMap.put(Global.RED_MONEY, c_min_last_money);
moneyList.add(moneyMap);
// 将剩余的最低金额放入List中
for (i = copies; i > 0; i--) {
moneyMap = new HashMap<String, Double>();
moneyMap.put(Global.RED_MONEY, min);
moneyList.add(moneyMap);
}
break;
}
// 最小红包金额等于0.01元时
if (Amount.div(money, copies) == min) {
// 将剩余的最低金额放入List中
for (i = copies; i > 0; i--) {
moneyMap = new HashMap<String, Double>();
moneyMap.put(Global.RED_MONEY, min);
moneyList.add(moneyMap);
}
break;
}
if (copies == 1) {
moneyMap = new HashMap<String, Double>();
moneyMap.put(Global.RED_MONEY, money);
moneyList.add(moneyMap);
break;
}
double max = Amount.div(money, (Amount.mul(copies, 0.5)));
Random r = new Random();
m = r.nextDouble() * max;
m = Math.floor(m * 100) / 100;
if (m < min) {
m = min;
}
money = Amount.sub(money, m);
moneyMap = new HashMap<String, Double>();
moneyMap.put(Global.RED_MONEY, m);
moneyList.add(moneyMap);
copies--;
}
return moneyList;
}
public static String getAccessToken() throws Exception {
try {
String url = Global.TOKEN;
System.out.println(">>>>>>>>>>>>>>>获取" + url);
JSONObject jsonObject = httpRequest(url, "GET", null);
System.out.println(jsonObject);
// 如果请求成功
if (null != jsonObject) {
try {
if ("7200".equals(jsonObject.getString("expires_in"))) {
return jsonObject.getString("access_token");
}
} catch (JSONException e) {
// 获取token失败
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr)
{
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try
{
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(null, requestUrl, new Handler());
HttpsURLConnection httpUrlConn = (HttpsURLConnection)url
.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) {
httpUrlConn.connect();
}
if (outputStr != null) {
OutputStream outputStream = httpUrlConn.getOutputStream();
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
public static String SHA1Encode(String sourceString)
{
String resultString = null;
try {
resultString = new String(sourceString);
MessageDigest md = MessageDigest.getInstance("SHA-1");
resultString = byte2hexString(md.digest(resultString.getBytes()));
} catch (Exception localException) {
}
return resultString;
}
public static final String byte2hexString(byte[] bytes)
{
StringBuffer buf = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
if ((bytes[i] & 0xFF) < 16) {
buf.append("0");
}
buf.append(Long.toString(bytes[i] & 0xFF, 16));
}
return buf.toString().toUpperCase();
}
public static int GetDistance(String lat1, String lng1, String lat2, String lng2)
{
double EARTH_RADIUS = 6378.1369999999997D;
double radLat1 = rad(Float.parseFloat(lat1));
double radLat2 = rad(Float.parseFloat(lat2));
double a = radLat1 - radLat2;
double b = rad(Float.parseFloat(lng1)) - rad(Float.parseFloat(lng2));
double s = 2.0D * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2.0D), 2.0D) +
Math.cos(radLat1) * Math.cos(radLat2) *
Math.pow(Math.sin(b / 2.0D), 2.0D)));
s *= EARTH_RADIUS;
s = Math.round(s * 10000.0D) / 10L;
return (int)s;
}
private static double rad(double d) {
return d * 3.141592653589793D / 180.0D;
}
public static String itudeConvert(String location)
throws Exception
{
double location_D = 0.0D;
if ((!"".equals(location)) && (location.indexOf("#") > 0)) {
String[] local = location.split("#");
location_D = Double.parseDouble(local[0]);
if (local.length > 1) {
location_D += Double.parseDouble(local[1]) / 60.0D;
}
if (local.length > 2) {
location_D += Double.parseDouble(local[2]) / 3600.0D;
}
}
return String.valueOf(location_D);
}
public static String getSignature(String timestamp, String nonce, String token)
throws Exception
{
String maxString = "";
String minString = "";
if (timestamp.compareTo(nonce) < 0) {
maxString = nonce;
minString = timestamp;
} else {
maxString = timestamp;
minString = nonce;
}
if (token.compareTo(maxString) > 0)
token = minString + maxString + token;
else if (token.compareTo(minString) < 0)
token = token + minString + maxString;
else {
token = minString + token + maxString;
}
return SHA1Encode(token);
}
public static String httpRequest(String requestUrl)
{
StringBuffer buffer = null;
try
{
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection)url
.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod("GET");
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
buffer = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
httpUrlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
public static String getRandomPwd()
{
long seed = 9999L;
long rand = seed + Math.round(Math.random() * 1000000.0D);
String pwd = String.valueOf(rand);
if (pwd.length() < 6)
pwd = String.valueOf(999999L - rand);
else {
pwd = pwd.substring(0, 6);
}
return pwd;
}
public static String getYearMonth()
throws Exception
{
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMM");
Calendar cal = Calendar.getInstance();
return dateformat.format(cal.getTime());
}
public static String getMonth()
throws Exception
{
SimpleDateFormat dateformat = new SimpleDateFormat("MM");
Calendar cal = Calendar.getInstance();
return dateformat.format(cal.getTime());
}
public static String convertTime(long time)
throws Exception
{
long second = time % 60L;
long minute = (time - second) % 3600L / 60L;
long hour = (time - second - minute * 60L) / 3600L;
return hour + "Сʱ" + minute + "��" + second + "��";
}
public static String convertFlow(long flow)
throws Exception
{
long bit = flow % 1024L;
long kByte = (flow - bit) % 1048576L / 1024L;
long mByte = (flow - bit - kByte * 1024L) / 1048576L;
return mByte + "MB " + kByte + "KB " + bit + "bit";
}
/**
* ��ȡһ��ȥ�����ַ�"-"��UUID
*
* @return
* @see [�ࡢ��#��������#��Ա]
*/
public static String getUUID() {
String s = UUID.randomUUID().toString();
// ȥ����-�����
return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)
+ s.substring(19, 23) + s.substring(24);
}
/**
* @author liangly
* @return ��ȡ������ĩ���ʱ�� ��ʽ��yyyyMMdd
* @throws Exception
*/
public static String getMonthLastDay() throws Exception {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.DATE, 1);
calendar.roll(Calendar.DATE, -1);
return sf.format(calendar.getTime()).toString();
}
/**
* @author
* @return �ϸ���ĩ���һ�� ��ʽ��yyyy-MM-dd
* @throws Exception
*/
public static String getLastMonthLastDay() throws Exception {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.DATE, -1);
calendar.roll(Calendar.DATE, 1);
return sf.format(calendar.getTime()).toString();
}
public static void printJson(HttpServletResponse response, String text) {
try {
print(response, "text/html;charset=UTF-8", text);
} catch (Exception ex) {
}
}
private static void print(HttpServletResponse response, String contentType, String text)
throws IOException
{
response.setContentType(contentType);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0L);
PrintWriter pw = response.getWriter();
pw.write(text);
pw.close();
}
public static String formatText(String toUserName, String fromUserName, String content)
{
String str = "";
str = String.format("<xml><ToUserName><![CDATA[%1$s]]></ToUserName><FromUserName><![CDATA[%2$s]]></FromUserName><CreateTime>%3$s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%4$s]]></Content><FuncFlag>0</FuncFlag></xml>", new Object[] {
fromUserName, toUserName, Long.valueOf((new Date()).getTime()), content
});
return str;
}
/**
*
* toGetData 判断是否要去取数据
*
* @param saveTime
* @return
* @throws Exception
* @return boolean返回说明
* @Exception 异常说明
* @author:zhangkun3
* @create:2014-1-21 上午11:42:31
* @moduser:
* @moddate:
* @remark:
*/
public static boolean toGetData(String saveTime, int delay) throws Exception {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// pm-time存放的是到网站取数据的时间,单位秒
calendar.add(Calendar.MINUTE, -delay);
return sf.parse(saveTime).before(sf.parse(sf.format(calendar.getTime())));
}
/**
* 去除html标签
*
* add by wuxw 2016-3-2
* @param htmlStr
* @return
*/
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //过滤script标签
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //过滤style标签
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll("").replace(" ", ""); //过滤html标签
return htmlStr.trim(); //返回文本字符串
}
}