|
@@ -0,0 +1,395 @@
|
|
1
|
+package com.liang.common.utils;
|
|
2
|
+
|
|
3
|
+import cn.hutool.core.date.DateTime;
|
|
4
|
+import cn.hutool.core.date.DateUtil;
|
|
5
|
+import cn.hutool.core.exceptions.ValidateException;
|
|
6
|
+import cn.hutool.core.lang.Validator;
|
|
7
|
+import cn.hutool.core.util.StrUtil;
|
|
8
|
+import cn.hutool.http.HttpException;
|
|
9
|
+import cn.hutool.http.HttpRequest;
|
|
10
|
+import cn.hutool.json.JSONObject;
|
|
11
|
+import cn.hutool.json.JSONUtil;
|
|
12
|
+import lombok.extern.slf4j.Slf4j;
|
|
13
|
+
|
|
14
|
+import java.io.*;
|
|
15
|
+import java.security.NoSuchAlgorithmException;
|
|
16
|
+import java.security.SecureRandom;
|
|
17
|
+import java.text.ParseException;
|
|
18
|
+import java.text.SimpleDateFormat;
|
|
19
|
+import java.util.*;
|
|
20
|
+import java.util.stream.Collectors;
|
|
21
|
+
|
|
22
|
+/**
|
|
23
|
+ * 说明:常用工具
|
|
24
|
+ */
|
|
25
|
+@Slf4j
|
|
26
|
+public class Tools {
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+ private static Random rand; // SecureRandom is preferred to Random
|
|
30
|
+
|
|
31
|
+ static {
|
|
32
|
+ try {
|
|
33
|
+ rand = SecureRandom.getInstanceStrong();
|
|
34
|
+ } catch (NoSuchAlgorithmException e) {
|
|
35
|
+ log.error("init error", e);
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+ private Tools() {
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ /**
|
|
45
|
+ * 随机生成六位数验证码
|
|
46
|
+ *
|
|
47
|
+ * @return
|
|
48
|
+ */
|
|
49
|
+ public static int getRandomNum() {
|
|
50
|
+// Random r = new Random();
|
|
51
|
+
|
|
52
|
+ return rand.nextInt(900000);//(Math.random()*(999999-100000)+100000)
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ /**
|
|
56
|
+ * 随机生成四位数验证码
|
|
57
|
+ *
|
|
58
|
+ * @return
|
|
59
|
+ */
|
|
60
|
+ public static int getRandomNum4() {
|
|
61
|
+// Random r = new Random();
|
|
62
|
+ return rand.nextInt(9000) + 1000;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+ /**
|
|
67
|
+ * 随机生成四位数验证码
|
|
68
|
+ *
|
|
69
|
+ * @return
|
|
70
|
+ */
|
|
71
|
+ public static int getRandomNumLen(int bound) {
|
|
72
|
+// Random r = new Random();
|
|
73
|
+ return rand.nextInt(bound);
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public static Random getRandom() {
|
|
77
|
+ return rand;
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ /**
|
|
81
|
+ * 往文件里的内容
|
|
82
|
+ *
|
|
83
|
+ * @param fileP 文件路径
|
|
84
|
+ * @param content 写入的内容
|
|
85
|
+ */
|
|
86
|
+ public static void writeFile(String fileP, String content) throws IOException {
|
|
87
|
+ String filePath = (Thread.currentThread().getContextClassLoader().getResource("")) + "../../"; //项目路径
|
|
88
|
+ filePath = filePath.replace("file:/", "");
|
|
89
|
+ filePath = filePath.replace("%20", " ");
|
|
90
|
+ filePath = filePath.trim() + fileP.trim();
|
|
91
|
+ if (filePath.indexOf(":") != 1) {
|
|
92
|
+ filePath = File.separator + filePath;
|
|
93
|
+ }
|
|
94
|
+ try (OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath), "utf-8");
|
|
95
|
+ BufferedWriter writer = new BufferedWriter(write);) {
|
|
96
|
+ writer.write(content);
|
|
97
|
+ } catch (IOException e) {
|
|
98
|
+ throw e;
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ public static String getString(Map map, String key) {
|
|
103
|
+ if (map == null || key == null) {
|
|
104
|
+ return "";
|
|
105
|
+ }
|
|
106
|
+ Object v = map.get(key);
|
|
107
|
+ return v == null ? "" : v.toString();
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ /**
|
|
111
|
+ * 检测字符串是否不为空(null,"","null")
|
|
112
|
+ *
|
|
113
|
+ * @param s
|
|
114
|
+ * @return 不为空则返回true,否则返回false
|
|
115
|
+ */
|
|
116
|
+ public static boolean notEmpty(String s) {
|
|
117
|
+ return !isEmpty(s);
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+ /**
|
|
122
|
+ * 集合是否为非空
|
|
123
|
+ *
|
|
124
|
+ * @param collection 集合
|
|
125
|
+ * @return 是否为非空
|
|
126
|
+ */
|
|
127
|
+ public static boolean notEmpty(Collection<?> collection) {
|
|
128
|
+ return !isEmpty(collection);
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+ /**
|
|
133
|
+ * 检测字符串是否不为空(null,"")
|
|
134
|
+ *
|
|
135
|
+ * @param obj
|
|
136
|
+ * @return 不为空则返回true,否则返回false
|
|
137
|
+ */
|
|
138
|
+ public static boolean notEmpty(Object obj) {
|
|
139
|
+ return !isEmpty(obj);
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+ /**
|
|
144
|
+ * 数组是否为非空
|
|
145
|
+ *
|
|
146
|
+ * @param <T> 数组元素类型
|
|
147
|
+ * @param array 数组
|
|
148
|
+ * @return 是否为非空
|
|
149
|
+ */
|
|
150
|
+ public static <T> boolean notEmpty(T[] array) {
|
|
151
|
+ return !isEmpty(array);
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ /**
|
|
155
|
+ * 检测字符串是否为空(null,"","null")
|
|
156
|
+ *
|
|
157
|
+ * @param s
|
|
158
|
+ * @return 为空则返回true,不否则返回false
|
|
159
|
+ */
|
|
160
|
+ public static boolean isEmpty(String s) {
|
|
161
|
+ return s == null || "".equals(s) || "null".equals(s);
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+ /**
|
|
166
|
+ * 集合是否为空
|
|
167
|
+ *
|
|
168
|
+ * @param collection 集合
|
|
169
|
+ * @return 是否为空
|
|
170
|
+ */
|
|
171
|
+ public static boolean isEmpty(Collection<?> collection) {
|
|
172
|
+ return collection == null || collection.isEmpty();
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ /**
|
|
176
|
+ * 检测字符串是否为空(null,"","null")
|
|
177
|
+ *
|
|
178
|
+ * @param obj
|
|
179
|
+ * @return 为空则返回true,不否则返回false
|
|
180
|
+ */
|
|
181
|
+ public static boolean isEmpty(Object obj) {
|
|
182
|
+ if (obj instanceof String) {
|
|
183
|
+ return isEmpty((String) obj);//TODO
|
|
184
|
+ } else {
|
|
185
|
+ return null == obj;
|
|
186
|
+ }
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ /**
|
|
190
|
+ * 数组是否为空
|
|
191
|
+ *
|
|
192
|
+ * @param <T> 数组元素类型
|
|
193
|
+ * @param array 数组
|
|
194
|
+ * @return 是否为空
|
|
195
|
+ */
|
|
196
|
+ public static <T> boolean isEmpty(T[] array) {
|
|
197
|
+ return array == null || array.length == 0;
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ /**
|
|
201
|
+ * 字符串转换为字符串数组
|
|
202
|
+ *
|
|
203
|
+ * @param str 字符串
|
|
204
|
+ * @param splitRegex 分隔符
|
|
205
|
+ * @return
|
|
206
|
+ */
|
|
207
|
+ public static String[] str2StrArray(String str, String splitRegex) {
|
|
208
|
+ if (isEmpty(str)) {
|
|
209
|
+ return null;
|
|
210
|
+ }
|
|
211
|
+ return str.split(splitRegex);
|
|
212
|
+ }
|
|
213
|
+
|
|
214
|
+ /**
|
|
215
|
+ * 用默认的分隔符(,)将字符串转换为字符串数组
|
|
216
|
+ *
|
|
217
|
+ * @param str 字符串
|
|
218
|
+ * @return
|
|
219
|
+ */
|
|
220
|
+ public static String[] str2StrArray(String str) {
|
|
221
|
+ return str2StrArray(str, ",\\s*");
|
|
222
|
+ }
|
|
223
|
+
|
|
224
|
+ /**
|
|
225
|
+ * 按照yyyy-MM-dd HH:mm:ss的格式,日期转字符串
|
|
226
|
+ *
|
|
227
|
+ * @param date
|
|
228
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
229
|
+ */
|
|
230
|
+ public static String date2Str(Date date) {
|
|
231
|
+ return date2Str(date, "yyyy-MM-dd HH:mm:ss");
|
|
232
|
+ }
|
|
233
|
+
|
|
234
|
+ /**
|
|
235
|
+ * 按照yyyy-MM-dd HH:mm:ss的格式,字符串转日期
|
|
236
|
+ *
|
|
237
|
+ * @param date
|
|
238
|
+ * @return
|
|
239
|
+ */
|
|
240
|
+ public static Date str2Date(String date) {
|
|
241
|
+ if (notEmpty(date)) {
|
|
242
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
243
|
+ try {
|
|
244
|
+ return sdf.parse(date);
|
|
245
|
+ } catch (ParseException e) {
|
|
246
|
+ log.error("", e);
|
|
247
|
+ }
|
|
248
|
+ return new Date();
|
|
249
|
+ } else {
|
|
250
|
+ return null;
|
|
251
|
+ }
|
|
252
|
+ }
|
|
253
|
+
|
|
254
|
+ /**
|
|
255
|
+ * 按照参数format的格式,日期转字符串
|
|
256
|
+ *
|
|
257
|
+ * @param date
|
|
258
|
+ * @param format
|
|
259
|
+ * @return
|
|
260
|
+ */
|
|
261
|
+ public static String date2Str(Date date, String format) {
|
|
262
|
+ if (date != null) {
|
|
263
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
264
|
+ return sdf.format(date);
|
|
265
|
+ } else {
|
|
266
|
+ return "";
|
|
267
|
+ }
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ /**
|
|
271
|
+ * 把时间根据时、分、秒转换为时间段
|
|
272
|
+ *
|
|
273
|
+ * @param StrDate
|
|
274
|
+ */
|
|
275
|
+ public static String getTimes(String StrDate) {
|
|
276
|
+ String resultTimes = "";
|
|
277
|
+
|
|
278
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
279
|
+ java.util.Date now;
|
|
280
|
+
|
|
281
|
+ try {
|
|
282
|
+ now = new Date();
|
|
283
|
+ java.util.Date date = df.parse(StrDate);
|
|
284
|
+ long times = now.getTime() - date.getTime();
|
|
285
|
+ long day = times / (24 * 60 * 60 * 1000);
|
|
286
|
+ long hour = (times / (60 * 60 * 1000) - day * 24);
|
|
287
|
+ long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
|
288
|
+ long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
|
289
|
+
|
|
290
|
+ StringBuffer sb = new StringBuffer();
|
|
291
|
+ //sb.append("发表于:");
|
|
292
|
+ if (hour > 0) {
|
|
293
|
+ sb.append(hour + "小时前");
|
|
294
|
+ } else if (min > 0) {
|
|
295
|
+ sb.append(min + "分钟前");
|
|
296
|
+ } else {
|
|
297
|
+ sb.append(sec + "秒前");
|
|
298
|
+ }
|
|
299
|
+
|
|
300
|
+ resultTimes = sb.toString();
|
|
301
|
+ } catch (ParseException e) {
|
|
302
|
+ log.error("", e);
|
|
303
|
+ }
|
|
304
|
+
|
|
305
|
+ return resultTimes;
|
|
306
|
+ }
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+ /**
|
|
310
|
+ * 读取txt里的全部内容
|
|
311
|
+ *
|
|
312
|
+ * @param fileP 文件路径
|
|
313
|
+ * @param encoding 编码
|
|
314
|
+ * @return
|
|
315
|
+ */
|
|
316
|
+ public static String readTxtFileAll(String fileP, String encoding) throws IOException {
|
|
317
|
+ StringBuffer fileContent = new StringBuffer();
|
|
318
|
+ try {
|
|
319
|
+ String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) + "../../"; //项目路径
|
|
320
|
+ filePath = filePath.replaceAll("file:/", "");
|
|
321
|
+ filePath = filePath.replaceAll("%20", " ");
|
|
322
|
+ filePath = filePath.trim() + fileP.trim();
|
|
323
|
+ if (filePath.indexOf(":") != 1) {
|
|
324
|
+ filePath = File.separator + filePath;
|
|
325
|
+ }
|
|
326
|
+ File file = new File(filePath);
|
|
327
|
+ if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
328
|
+ try (InputStreamReader read = new InputStreamReader(
|
|
329
|
+ new FileInputStream(file), encoding); // 考虑到编码格式
|
|
330
|
+ BufferedReader bufferedReader = new BufferedReader(read);) {
|
|
331
|
+ String lineTxt;
|
|
332
|
+ while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
333
|
+ fileContent.append(lineTxt);
|
|
334
|
+ fileContent.append("\n");
|
|
335
|
+ }
|
|
336
|
+ }
|
|
337
|
+ } else {
|
|
338
|
+ throw new FileNotFoundException("找不到指定的文件,查看此路径是否正确:" + filePath);
|
|
339
|
+ }
|
|
340
|
+ } catch (IOException e) {
|
|
341
|
+ throw new IOException("读取文件内容出错" + e.getMessage());
|
|
342
|
+ }
|
|
343
|
+ return fileContent.toString();
|
|
344
|
+ }
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+ public static boolean isEmail(String email) {
|
|
348
|
+ return Validator.isMatchRegex("^[A-Za-z0-9+_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$", email);
|
|
349
|
+ }
|
|
350
|
+
|
|
351
|
+ public static boolean isMobile(String phone) {
|
|
352
|
+ return Validator.isMobile(phone);
|
|
353
|
+ }
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+ /**
|
|
357
|
+ * 获取周末 月从0开始
|
|
358
|
+ *
|
|
359
|
+ * @param year
|
|
360
|
+ * @param mouth
|
|
361
|
+ * @return
|
|
362
|
+ */
|
|
363
|
+ public static Set<String> getMonthWekDay(int year, int mouth) {
|
|
364
|
+ Set<String> dateList = new HashSet<>();
|
|
365
|
+ SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
366
|
+ Calendar calendar = new GregorianCalendar(year, mouth, 1);
|
|
367
|
+ Calendar endCalendar = new GregorianCalendar(year, mouth, 1);
|
|
368
|
+ endCalendar.add(Calendar.MONTH, 1);
|
|
369
|
+ while (true) {
|
|
370
|
+ int weekday = calendar.get(Calendar.DAY_OF_WEEK);
|
|
371
|
+ if (weekday == 1 || weekday == 7) {
|
|
372
|
+ dateList.add(simdf.format(calendar.getTime()));
|
|
373
|
+ }
|
|
374
|
+ calendar.add(Calendar.DATE, 1);
|
|
375
|
+ if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
|
|
376
|
+ break;
|
|
377
|
+ }
|
|
378
|
+ }
|
|
379
|
+ return dateList;
|
|
380
|
+ }
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+ /**
|
|
384
|
+ * @param obj 实体
|
|
385
|
+ * @return 全部不为空并且全部不为null, 则返回true;否则返回false
|
|
386
|
+ */
|
|
387
|
+ public static boolean allNotEmpty(Object... obj) {
|
|
388
|
+ for (Object o : obj) {
|
|
389
|
+ if (Tools.isEmpty(o)) {
|
|
390
|
+ return false;
|
|
391
|
+ }
|
|
392
|
+ }
|
|
393
|
+ return true;
|
|
394
|
+ }
|
|
395
|
+}
|