1. ZipUtil

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
package top.bingcu.utils;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
* @author BinGCU
*/
public class ZipUtil {

/**
* 压缩文件-由于out要在递归调用外,所以封装一个方法用来
* 调用ZipFiles(ZipOutputStream out,String path,File... srcFiles)
* @param zip
* @param path
* @param srcFiles
* @throws IOException
* @author isea533
*/
public static void ZipFiles(File zip,String path,File... srcFiles) throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
ZipUtil.ZipFiles(out,path,srcFiles);
out.close();
System.out.println("*****************压缩完毕*******************");
}


/**
* 压缩文件-File
* @param srcFiles 被压缩源文件
* @author isea533
*/
private static void ZipFiles(ZipOutputStream out,String path,File... srcFiles){
path = path.replaceAll("\\\\", "/");
if(!path.endsWith("/")){
path+="/";
}
byte[] buf = new byte[1024];
try {
for(int i=0;i<srcFiles.length;i++){
if(srcFiles[i].isDirectory()){
File[] files = srcFiles[i].listFiles();
String srcPath = srcFiles[i].getName();
srcPath = srcPath.replaceAll("\\\\", "/");
if(!srcPath.endsWith("/")){
srcPath+="/";
}
out.putNextEntry(new ZipEntry(path+srcPath));
ZipFiles(out,path+srcPath,files);
}
else{
FileInputStream in = new FileInputStream(srcFiles[i]);
System.out.println(path + srcFiles[i].getName());
out.putNextEntry(new ZipEntry(path + srcFiles[i].getName()));
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.closeEntry();
in.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 解压到指定目录
* @param zipPath
* @param descDir
* @author isea533
*/
public static void unZipFiles(String zipPath,String descDir)throws IOException{
//处理路径传进来时的"\"为"/", 无则添加
descDir.replaceAll("\\\\", "/");
Character lastChar = descDir.charAt(descDir.length() - 1);
if (!lastChar.equals('/'))
descDir += "/";
// else
// if (!lastChar.equals("\\"))
// descDir += "\\";

unZipFiles(new File(zipPath), descDir);
}
/**
* 解压文件到指定目录
* @param zipFile
* @param descDir
* @author isea533
*/
@SuppressWarnings("rawtypes")
private static void unZipFiles(File zipFile,String descDir)throws IOException{
File pathFile = new File(descDir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for(Enumeration entries = zip.entries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (descDir+zipEntryName).replaceAll("\\\\", "/");
// String outPath = new File(descDir, zipEntryName).getAbsolutePath();
//判断路径是否存在,不存在则创建文件路径
// File file = new File(outPath.substring(0, outPath.lastIndexOf('\\')));
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
//输出文件路径信息
System.out.println(outPath);

OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
entry.clone();
}
zip.close();
System.out.println("******************解压完毕********************");
}
}

2. FileUtil

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
package top.bingcu.utils;

import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* @author BinGCU
*/
public class FileUtil {

public static MultipartFile read(File file) throws IOException {
MultipartFile mulFile = new MockMultipartFile(
file.getName(), //文件名
file.getName(), //originalName 相当于上传文件在客户机上的文件名
ContentType.APPLICATION_OCTET_STREAM.toString(), //文件类型
new FileInputStream(file) //文件流
);

return mulFile;
}

public static void removeFileOrDirectory(File file) {
File[] files = file.listFiles();//将file子目录及子文件放进文件数组
if (files != null) {//如果包含文件进行删除操作
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {//删除子文件
files[i].delete();
} else if (files[i].isDirectory()) {//通过递归方法删除子目录的文件
removeFileOrDirectory(files[i]);
}
files[i].delete();//删除子目录
}
}
file.delete();
}

public static Boolean saveFileTo(MultipartFile file, String absoluteDestDirPath, String fileName){
return saveFileTo(file, new File(absoluteDestDirPath, fileName).getAbsolutePath());
}

public static Boolean saveFileTo(MultipartFile file, String absoluteDestFilePath){
try {
file.transferTo(new File(absoluteDestFilePath));
} catch (IOException e) {
e.printStackTrace();
return false;
}

return true;
}

}

3. RedisLockUtil

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
package top.bingcu.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
* Created by jayHaw
* 2017-08-07 23:55
*/
@Component
@Slf4j
public class RedisLockUtil {

@Autowired
private StringRedisTemplate redisTemplate;

/**
* 加锁
* @param key
* @param value 当前时间+超时时间
* @return
*/
public boolean lock(String key, String value) {
if(redisTemplate.opsForValue().setIfAbsent(key, value)) {
return true;
}
//currentValue=A 这两个线程的value都是B 其中一个线程拿到锁
String currentValue = redisTemplate.opsForValue().get(key);
//如果锁过期
if (!StringUtils.isEmpty(currentValue)
&& Long.parseLong(currentValue) < System.currentTimeMillis()) {
//获取上一个锁的时间
String oldValue = redisTemplate.opsForValue().getAndSet(key, value);
if (!StringUtils.isEmpty(oldValue) && oldValue.equals(currentValue)) {
return true;
}
}

return false;
}

/**
* 解锁
* @param key
* @param value
*/
public void unlock(String key, String value) {
try {
String currentValue = redisTemplate.opsForValue().get(key);
if (!StringUtils.isEmpty(currentValue) && currentValue.equals(value)) {
redisTemplate.opsForValue().getOperations().delete(key);
}
}catch (Exception e) {
log.error("【redis分布式锁】解锁异常, {}", e);
}
}

}

4. MongodbUtil

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
package top.bingcu.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author codekiller
* @date 2020/5/30 15:53
* Mongodb工具类
*/
@Component
public class MongodbUtil {

public static MongodbUtil mongodbUtils;

@PostConstruct
public void init() {
mongodbUtils = this;
mongodbUtils.mongoTemplate = this.mongoTemplate;
}

@Autowired
private MongoTemplate mongoTemplate;

/**
* 保存数据对象,集合为数据对象中@Document 注解所配置的collection
*
* @param obj 数据对象
*/
public static void save(Object obj) {
mongodbUtils.mongoTemplate.save(obj);
}

/**
* 指定集合保存数据对象
*
* @param obj 数据对象
* @param collectionName 集合名
*/
public static void save(Object obj, String collectionName) {

mongodbUtils.mongoTemplate.save(obj, collectionName);
}

/**
* 根据数据对象中的id删除数据,集合为数据对象中@Document 注解所配置的collection
*
* @param obj 数据对象
*/
public static void remove(Object obj) {

mongodbUtils.mongoTemplate.remove(obj);
}

/**
* 指定集合 根据数据对象中的id删除数据
*
* @param obj 数据对象
* @param collectionName 集合名
*/
public static void remove(Object obj, String collectionName) {

mongodbUtils.mongoTemplate.remove(obj, collectionName);
}

/**
* 根据key,value到指定集合删除数据
*
* @param key 键
* @param value 值
* @param collectionName 集合名
*/
public static void removeById(String key, Object value, String collectionName) {

Criteria criteria = Criteria.where(key).is(value);
criteria.and(key).is(value);
Query query = Query.query(criteria);
mongodbUtils.mongoTemplate.remove(query, collectionName);
}

/**
* 指定集合 修改数据,且仅修改找到的第一条数据
*
* @param accordingKey 修改条件 key
* @param accordingValue 修改条件 value
* @param updateKeys 修改内容 key数组
* @param updateValues 修改内容 value数组
* @param collectionName 集合名
*/
public static void updateFirst(String accordingKey, Object accordingValue, String[] updateKeys, Object[] updateValues,
String collectionName) {

Criteria criteria = Criteria.where(accordingKey).is(accordingValue);
Query query = Query.query(criteria);
Update update = new Update();
for (int i = 0; i < updateKeys.length; i++) {
update.set(updateKeys[i], updateValues[i]);
}
mongodbUtils.mongoTemplate.updateFirst(query, update, collectionName);
}

/**
* 指定集合 修改数据,且修改所找到的所有数据
*
* @param accordingKey 修改条件 key
* @param accordingValue 修改条件 value
* @param updateKeys 修改内容 key数组
* @param updateValues 修改内容 value数组
* @param collectionName 集合名
*/
public static void updateMulti(String accordingKey, Object accordingValue, String[] updateKeys, Object[] updateValues,
String collectionName) {

Criteria criteria = Criteria.where(accordingKey).is(accordingValue);
Query query = Query.query(criteria);
Update update = new Update();
for (int i = 0; i < updateKeys.length; i++) {
update.set(updateKeys[i], updateValues[i]);
}
mongodbUtils.mongoTemplate.updateMulti(query, update, collectionName);
}

/**
* 根据条件查询出所有结果集 集合为数据对象中@Document 注解所配置的collection
*
* @param obj 数据对象
* @param findKeys 查询条件 key
* @param findValues 查询条件 value
* @return
*/
public static List<? extends Object> find(Object obj, String[] findKeys, Object[] findValues) {

Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
List<? extends Object> resultList = mongodbUtils.mongoTemplate.find(query, obj.getClass());
return resultList;
}

/**
* 指定集合 根据条件查询出所有结果集
*
* @param obj 数据对象
* @param findKeys 查询条件 key
* @param findValues 查询条件 value
* @param collectionName 集合名
* @return
*/
public static List<? extends Object> find(Object obj, String[] findKeys, Object[] findValues, String collectionName) {

Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
List<? extends Object> resultList = mongodbUtils.mongoTemplate.find(query, obj.getClass(), collectionName);
return resultList;
}

/**
* 指定集合 根据条件查询出所有结果集 并排倒序
*
* @param obj 数据对象
* @param findKeys 查询条件 key
* @param findValues 查询条件 value
* @param collectionName 集合名
* @param sort 排序字段
* @return
*/
public static List<? extends Object> find(Object obj, String[] findKeys, Object[] findValues, String collectionName, String sort) {

Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
query.with(Sort.by(Direction.DESC, sort));
List<? extends Object> resultList = mongodbUtils.mongoTemplate.find(query, obj.getClass(), collectionName);
return resultList;
}

/**
* 根据条件查询出符合的第一条数据 集合为数据对象中 @Document 注解所配置的collection
*
* @param obj 数据对象
* @param findKeys 查询条件 key
* @param findValues 查询条件 value
* @return
*/
public static Object findOne(Object obj, String[] findKeys, Object[] findValues) {

Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
Object resultObj = mongodbUtils.mongoTemplate.findOne(query, obj.getClass());
return resultObj;
}

/**
* 指定集合 根据条件查询出符合的第一条数据
*
* @param obj 数据对象
* @param findKeys 查询条件 key
* @param findValues 查询条件 value
* @param collectionName 集合名
* @return
*/
public static Object findOne(Object obj, String[] findKeys, Object[] findValues, String collectionName) {

Criteria criteria = null;
for (int i = 0; i < findKeys.length; i++) {
if (i == 0) {
criteria = Criteria.where(findKeys[i]).is(findValues[i]);
} else {
criteria.and(findKeys[i]).is(findValues[i]);
}
}
Query query = Query.query(criteria);
Object resultObj = mongodbUtils.mongoTemplate.findOne(query, obj.getClass(), collectionName);
return resultObj;
}

/**
* 查询出所有结果集 集合为数据对象中 @Document 注解所配置的collection
*
* @param obj 数据对象
* @return
*/
public static List<? extends Object> findAll(Object obj) {

List<? extends Object> resultList = mongodbUtils.mongoTemplate.findAll(obj.getClass());
return resultList;
}

/**
* 查询出所有结果集 集合为数据对象中 @Document 注解所配置的collection
*
* @param clazz
* @param <T>
* @return
*/
public static <T> List<T> findAll(Class<T> clazz) {
List<T> resultList = mongodbUtils.mongoTemplate.findAll(clazz);
return resultList;
}

/**
* 指定集合 查询出所有结果集
*
* @param obj 数据对象
* @param collectionName 集合名
* @return
*/
public static List<? extends Object> findAll(Object obj, String collectionName) {

List<? extends Object> resultList = mongodbUtils.mongoTemplate.findAll(obj.getClass(), collectionName);
return resultList;
}

/**
* 指定集合 查询出所有结果集
*
* @param clazz
* @param collectionName
* @param <T>
* @return
*/
public static <T> List<T> findAll(Class<T> clazz, String collectionName) {
List<T> resultList = mongodbUtils.mongoTemplate.findAll(clazz, collectionName);
return resultList;
}

}

5. DateUtil

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
package top.bingcu.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
/**
* 时间戳转换成日期格式字符串
* @param seconds 精确到秒的字符串
* @param
* @return
*/
public static String timeStamp2Date(String seconds,String format) {
if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
return "";
}
if(format == null || format.isEmpty()){
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(seconds+"000")));
}
/**
* 日期格式字符串转换成时间戳
* @param
* @param format 如:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String date2TimeStamp(String date_str,String format){
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return String.valueOf(sdf.parse(date_str).getTime()/1000);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

/**
* 取得当前时间戳(精确到秒)
* @return
*/
public static String timeStamp(){
long time = System.currentTimeMillis();
String t = String.valueOf(time/1000);
return t;
}

// public static void main(String[] args) {
// String timeStamp = timeStamp();
// System.out.println("timeStamp="+timeStamp); //运行输出:timeStamp=1470278082
// System.out.println(System.currentTimeMillis());//运行输出:1470278082980
// //该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数
//
// String date = timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss");
// System.out.println("date="+date);//运行输出:date=2016-08-04 10:34:42
//
// String timeStamp2 = date2TimeStamp(date, "yyyy-MM-dd HH:mm:ss");
// System.out.println(timeStamp2); //运行输出:1470278082
// }
}

6. EmailUtil

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
package top.bingcu.utils;

import io.huayu.springboot.bookmanage.entity.Config;
import io.huayu.springboot.framework.common.exception.BusinessException;
import io.huayu.springboot.framework.resource.sendmail.MyJavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.Properties;

/**
* @author Arlene
* @version 0.0.
* @description EmailUtil
* @since 2020/12/15 18:24
*/
public class EmailUtil {

public static void sendEmail(Map<Long, Config> configMap,
String to,
String content,
String title) throws Exception {
Config emailServerConfig = configMap.get(1L); // 邮箱服务器
Config emailAccountConfig = configMap.get(2L); // 邮箱账号
Config emailPasswordConfig = configMap.get(3L); // 邮箱密码
Config emailPortConfig = configMap.get(4L); // 服务端口

if (emailServerConfig == null || emailAccountConfig == null ||
emailPasswordConfig == null || emailPortConfig == null) {
throw new BusinessException("");
}

// 配置邮箱信息
MyJavaMailSender sender = new MyJavaMailSender();
Properties senderProperties = new Properties();
senderProperties.put("mail.smtp.auth", true);
senderProperties.put("mail.smtp.timeout", "10000");// 超时设置
senderProperties.put("mail.smtp.starttls.enable", true);
senderProperties.put("mail.smtp.starttls.required", true);
senderProperties.put("mail.smtp.ssl.enable", true);
senderProperties.put("mail.smtp.socketFactory.port", emailPortConfig.getConfigValue());
senderProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
sender.setHost(emailServerConfig.getConfigValue());
sender.setUsername(emailAccountConfig.getConfigValue());
sender.setPassword(emailPasswordConfig.getConfigValue());
sender.setPort(Integer.valueOf(emailPortConfig.getConfigValue()));
sender.setProtocol("smtp");
sender.setDefaultEncoding("UTF-8");
sender.setJavaMailProperties(senderProperties);


MimeMessage mailMessage = sender.createMimeMessage();
MimeMessageHelper helper;

helper = new MimeMessageHelper(mailMessage, true);
helper.setFrom(emailAccountConfig.getConfigValue());
helper.setTo(to);
helper.setSubject(title);
helper.setText(content);

// 发送邮件
sender.send(mailMessage);
}
}

7. ObjectUtil

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
package top.bingcu.utils;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
* 判断对象是否为空或null
*/
public class ObjectUtil {

public static boolean isNull(Object obj) {
return obj == null;
}

public static boolean isNotNull(Object obj) {
return !isNull(obj);
}

public static boolean isEmpty(Object obj) {
if (obj == null) return true;
else if (obj instanceof CharSequence) return ((CharSequence) obj).length() == 0;
else if (obj instanceof Collection) return ((Collection) obj).isEmpty();
else if (obj instanceof Map) return ((Map) obj).isEmpty();
else if (obj.getClass().isArray()) return Array.getLength(obj) == 0;

return false;
}

public static boolean isNotEmpty(Object obj) {
return !isEmpty(obj);
}


/**
* 将object转为Integer类型
* @param object
* @return
*/
public static Integer getIntegerByObject(Object object){
Integer in = null;

if(object!=null){
if(object instanceof Integer){
in = (Integer)object;
}else if(object instanceof String){
in = Integer.parseInt((String)object);
}else if(object instanceof Double){
in = (int)((double)object);
}else if(object instanceof Float){
in = (int)((float)object);
}else if(object instanceof BigDecimal){
in = ((BigDecimal)object).intValue();
}else if(object instanceof Long){
in = ((Long)object).intValue();
}
}

return in;
}


/**
* 去除字符串首尾出现的某个字符.
* @param source 源字符串.
* @param element 需要去除的字符.
* @return String.
*/
public static String trimFirstAndLastChar(String source, char element){
boolean beginIndexFlag = true;
boolean endIndexFlag = true;
do{
int beginIndex = source.indexOf(element) == 0 ? 1 : 0;
int endIndex = source.lastIndexOf(element) + 1 == source.length() ? source.lastIndexOf(element) : source.length();
source = source.substring(beginIndex, endIndex);
beginIndexFlag = (source.indexOf(element) == 0);
endIndexFlag = (source.lastIndexOf(element) + 1 == source.length());
} while (beginIndexFlag || endIndexFlag);
return source;
}


/*
* 将时间转换为时间戳
*/
public static String dateToStamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}

/*
* 将时间戳转换为时间
*/
public static String stampToDate(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
}

8. Md5Util

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
package top.bingcu.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {

public static String md5(String plainText) {
//定义一个字节数组
byte[] secretBytes = null;
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
//对字符串进行加密
md.update(plainText.getBytes());
//获得加密后的数据
secretBytes = md.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
//将加密后的数据转换为16进制数字
String md5code = new BigInteger(1, secretBytes).toString(16);
// 如果生成数字未满32位,需要前面补0
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}

}

9. AESUtil

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
package top.bingcu.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class AESUtil {
// private static final String key = "aesEncryptionKey";
// private static final String initVector = "encryptionIntVec";

//加密
public static String encrypt(String key, String initVector, String value) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}


//解密
public static String decrypt(String key, String initVector, String encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}

return null;
}

public static String getKey() {
try {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128);
//要生成多少位,只需要修改这里即可128, 192或256
SecretKey sk = kg.generateKey();
byte[] b = sk.getEncoded();
String s = byteToHexString(b);
System.out.println(s);
System.out.println("十六进制密钥长度为" + s.length());
System.out.println("二进制密钥的长度为" + s.length() * 4);
return s;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.out.println("没有此算法。");
}
return null;
}

public static String byteToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String strHex = Integer.toHexString(bytes[i]);
if (strHex.length() > 3) {
sb.append(strHex.substring(6));
} else {
if (strHex.length() < 2) {
sb.append("0" + strHex);
} else {
sb.append(strHex);
}
}
}
return sb.toString();
}


public static String getStringRandom() {
String val = "";
Random random = new Random();
//参数lengths,表示生成几位随机数
for (int i = 0; i < 16; i++) {
String strOrNum = random.nextInt(2) % 2 == 0 ? "str" : "num";
//随机输出是字母还是数字
if ("str".equalsIgnoreCase(strOrNum)) {
//随机输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (random.nextInt(26) + temp);
} else if ("num".equalsIgnoreCase(strOrNum)) {
val += String.valueOf(random.nextInt(10));
}
}
return val;

}


/**
* 随机16位数字作为key
*/
public static String keyNum() {
String chars = "0123456789";
char[] rands = new char[16];
for (int i = 0; i < 16; i++) {
int rand = (int) (Math.random() * 10);
rands[i] = chars.charAt(rand);
}
String a = String.valueOf(rands);
return a;
}

/**
* 随机16位字母作为iv
*/
public static String iivEnglish() {
//需要生成几位
int n = 16;
//最终生成的字符串
String str = "";
for (int i = 0; i < n; i++) {
str = str + (char) (Math.random() * 26 + 'a');
}

return str;
}

}

10. JsonUtil

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
package top.bingcu.utils;


import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

/**
* @author Arlene
* @version 0.0.
* @description JsonUtil
* @since 2020/12/24 23:40
*/
public class JsonUtil {
private static final Logger logger = (Logger) LoggerFactory.getLogger(JsonUtil.class);

private static ObjectMapper objectMapper = new ObjectMapper();

/**
* 将对象序列化为JSON字符串
*
* @param object
* @return JSON字符串
*/
public static String serialize(Object object) {
Writer write = new StringWriter();
try {
objectMapper.writeValue(write, object);
} catch (JsonGenerationException e) {
logger.error("JsonGenerationException when serialize object to json", e);
} catch (JsonMappingException e) {
logger.error("JsonMappingException when serialize object to json", e);
} catch (IOException e) {
logger.error("IOException when serialize object to json", e);
}
return write.toString();
}

/**
* 将JSON字符串反序列化为对象
*
* @param json
* @param clazz
* @return JSON字符串
*/
public static <T> T deserialize(String json, Class<T> clazz) {
Object object = null;
try {
object = objectMapper.readValue(json, TypeFactory.rawClass(clazz));
} catch (JsonParseException e) {
logger.error("JsonParseException when serialize object to json", e);
} catch (JsonMappingException e) {
logger.error("JsonMappingException when serialize object to json", e);
} catch (IOException e) {
logger.error("IOException when serialize object to json", e);
}
return (T) object;
}

/**
* 将JSON字符串反序列化为对象
*
* @param json
* @param typeRef
* @return JSON字符串
*/
public static <T> T deserialize(String json, TypeReference<T> typeRef) {
try {
return (T) objectMapper.readValue(json, typeRef);
} catch (JsonParseException e) {
logger.error("JsonParseException when deserialize json", e);
} catch (JsonMappingException e) {
logger.error("JsonMappingException when deserialize json", e);
} catch (IOException e) {
logger.error("IOException when deserialize json", e);
}
return null;
}
}

11. RandomNumUtil

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
package top.bingcu.utils;

/**
* @author Arlene
* @version 0.0.
* @description RandomNumUtil
* @since 2020/12/12 17:45
*/
public class RandomNumUtil {
//获取m~n范围内的整数
public static int getRandom(int m,int n){
int random=(int)(Math.random()*(n-m))+m;
return random;
}

//获取位数为n的随机数
public static int getRandom(int length){
int m=getNumber(length);
int n=m*10-1;
int random=(int)(Math.random()*(n-m))+m;
return random;
}

public static int getNumber(int n){
if(n<1){
n=1;
}
if(n==1){
return 1;
}else{
n=n-1;
return 10*getNumber(n);
}
}
}

12. RandomStringUtil

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
package top.bingcu.utils;

import java.security.SecureRandom;
import java.util.Random;

/**
* @author Arlene
* @version 0.0.
* @description RandomStringUtil
* @since 2020/12/6 22:45
*/
public class RandomStringUtil {

public static Random rd = new SecureRandom();

// 随机生成16位数字组成的字符串
public static String getRanNumKey() {
StringBuilder str = new StringBuilder();
//产生16位的强随机数
for (int i = 0; i < 16; i++) {
//0-9的随机数
str.append(getRandomNumChar());
}
return str.toString();
}

// 随机生成16位英文字符组成的字符串
public static String getEngCharKey() {
StringBuilder str = new StringBuilder();
//产生16位的强随机数
for (int i = 0; i < 16; i++) {
str.append(getRandomEngChar(rd.nextInt(2)));
}
return str.toString();
}

// 随机生成大写英文与数字字符组成的字符串
public static String getNumAndBigEngCharKey(Integer size) {
StringBuilder str = new StringBuilder();
//产生指定位数的强随机数
for (int i = 0; i < size; i++) {
//产生0-1的随机数
int type = rd.nextInt(2);
if (type == 0) {
str.append(getRandomNumChar());
} else {
str.append(getRandomEngChar(rd.nextInt(2)));
}
}
return str.toString();
}

public static char getRandomNumChar() {
return (char)(rd.nextInt(10)+48);
}

// type为0时产生大写英文字符,
// type为1时产生小写英文字符
public static char getRandomEngChar(int type) {
if(type == 0) {
//ASCII在65-90之间为大写,获取大写随机
return (char)(rd.nextInt(25)+65);
} else {
//ASCII在97-122之间为小写,获取小写随机
return (char)(rd.nextInt(25)+97);
}
}
}