Java-开发常用工具集
Java-开发常用工具集
Lombok
-
依赖
1
2
3
4<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency> -
注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 全参构造
// 无参构造
public class User {
private String userName;
private Integer age;
}
// @Data: 生成如下方法
See Also:
Getter,
Setter,
RequiredArgsConstructor,
ToString,
EqualsAndHashCode,
Value -
原理
原理 -
注解解析
-
@Data
-
@Accessors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 开启链式调用
public class User {
private String userName;
private Integer age;
}
// 链式调用实例
public class UserDao {
public static void main(String[] args) {
User user = new User();
user.setUserName("coder-itl")
.setAge(18);
}
}
-
Hutool
-
包含组件
一个
Java 基础工具类,对文件、流、加密解密、转码、正则、线程、XML 等 JDK 方法进行封装,组成各种 Util 工具类,同时提供以下组件: 模块 介绍 hutool-aop JDK 动态代理封装,提供非 IOC 下的切面支持 hutool-bloomFilter 布隆过滤,提供一些 Hash 算法的布隆过滤 hutool-cache 简单缓存实现 hutool-core 核心,包括 Bean 操作、日期、各种 Util 等 hutool-cron 定时任务模块,提供类 Crontab 表达式的定时任务 hutool-crypto 加密解密模块,提供对称、非对称和摘要算法封装 hutool-db JDBC 封装后的数据操作,基于 ActiveRecord 思想 hutool-dfa 基于 DFA 模型的多关键字查找 hutool-extra 扩展模块,对第三方封装(模板引擎、邮件、Servlet、二维码、Emoji、FTP、分词等) hutool-http 基于 HttpUrlConnection 的 Http 客户端封装 hutool-log 自动识别日志实现的日志门面 hutool-script 脚本执行封装,例如 Javascript hutool-setting 功能更强大的 Setting 配置文件和 Properties 封装 hutool-system 系统参数调用封装(JVM 信息等) hutool-json JSON 实现 hutool-captcha 图片验证码实现 hutool-poi 针对 POI 中 Excel 和 Word 的封装 hutool-socket 基于 Java 的 NIO 和 AIO 的 Socket 封装 hutool-jwt JSON Web Token (JWT) 封装实现 -
依赖
1
2
3
4
5
6
7<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.5</version>
</dependency> -
Convert
1
import cn.hutool.core.convert.Convert;
-
转换为字符串
1
2
3
4
5
6
7
8
9
10
void testConvertToStr() {
// 数字转换为字符串
String toStr = Convert.toStr(1);
System.out.println("toStr = " + toStr); // toStr = 1
// 数组转化为字符串
long[] array = {1,2,3,4,5};
String arrayToStr = Convert.toStr(array);
System.out.println("arrayToStr = " + arrayToStr); // arrayToStr = [1, 2, 3, 4, 5]
} -
转换为指定类型数组
1
2
3
4
5
6
7
8
9
void testConvertArrayToOtherType() {
// 将数组转换为其他指定类型
long[] array = {1,2,3,4,5};
Integer[] integersArray = Convert.toIntArray(array);
for (Integer item : integersArray) {
System.out.println(item);
}
} -
转换为日期对象
1
2
3
4
5
6
7
8
9
void testConvertDate() throws ParseException {
String dateInfo = "2022-12-01";
Date parseDateInfo = new SimpleDateFormat("yyyy-MM-dd").parse(dateInfo);
System.out.println("parseDateInfo = " + parseDateInfo);
// 将字符串转换为日期格式
Date date = Convert.toDate(dateInfo);
System.out.println(date);
} -
数组转集合
1
2
3
4
5
6
7
8
9
10
void testConvertArrayToList() throws ParseException {
// 将数组转换为集合
long[] array = {1, 2, 3, 4, 5};
// 1. asList
List<long[]> longs = Arrays.asList(array);
// 2. Convert.
List<Long> objects = (List<Long>) Convert.toList(array);
objects.forEach(s -> System.out.println(s));
}
-
-
IO
流 -
文件拷贝
-
原始方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void testIO() throws IOException {
FileInputStream is = new FileInputStream(new File("E:\\springboot-demo\\springboot-junit\\src\\main\\resources\\uploadFile\\init.md"));
FileOutputStream os = new FileOutputStream(new File("E:\\springboot-demo\\springboot-junit\\src\\main\\resources\\uploadFile\\init3.md"));
int len = 0;
while (true) {
// 定义缓冲区
byte[] bytes = new byte[1024];
len = is.read(bytes);
if (len == -1) break;
os.write(bytes, 0, len);
}
is.close();
os.close();
} -
工具类
1
2
3
4
5
6
7
8
void testIO() throws FileNotFoundException {
FileInputStream is = new FileInputStream(new File("E:\\springboot-demo\\springboot-junit\\src\\main\\resources\\uploadFile\\init.md"));
FileOutputStream os = new FileOutputStream(new File("E:\\springboot-demo\\springboot-junit\\src\\main\\resources\\uploadFile\\init2.md"));
// 一行语句
IoUtil.copy(is, os);
}
-
-
-
md5
1
2
3
4
5
void testMD5() {
String md5 = SecureUtil.md5("123456");
System.out.println("md5 = " + md5);
} -
jWT
-
JWT
创建 1
2
3
4
5
6
7
8
9Map<String, Object> map = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("uid", Integer.parseInt("123"));
put("expire_time", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15);
}
};
JWTUtil.createToken(map, "1234".getBytes()); -
JWT
解析 1
2
3
4
5
6
7
8String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
"eyJzdWIiOiIxMjM0NTY3ODkwIiwiYWRtaW4iOnRydWUsIm5hbWUiOiJsb29seSJ9." +
"U2aQkC2THYV9L0fTN-yBBI7gmo5xhmvMhATtu8v0zEA";
final JWT jwt = JWTUtil.parseToken(rightToken);
jwt.getHeader(JWTHeader.TYPE);
jwt.getPayload("sub"); -
JWT
验证 1
2
3
4
5String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
"eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJleHAiOjE2MjQwMDQ4MjIsInVzZXJJZCI6MSwiYXV0aG9yaXRpZXMiOlsiUk9MRV_op5LoibLkuozlj7ciLCJzeXNfbWVudV8xIiwiUk9MRV_op5LoibLkuIDlj7ciLCJzeXNfbWVudV8yIl0sImp0aSI6ImQ0YzVlYjgwLTA5ZTctNGU0ZC1hZTg3LTVkNGI5M2FhNmFiNiIsImNsaWVudF9pZCI6ImhhbmR5LXNob3AifQ." +
"aixF1eKlAKS_k3ynFnStE7-IRGiD5YaqznvK2xEjBew";
JWTUtil.verify(token, "123456".getBytes());
-
-
图形验证码
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 coder-itl!
评论