Hutool工具类使用说明
Hutool是一个Java工具类库,提供了丰富的功能模块,包括字符串处理、日期时间操作、IO流、加密解密、HTTP客户端等。以下是一些常用模块的具体使用方法。
字符串工具(StrUtil)
字符串处理是开发中的常见需求,StrUtil
模块提供了高效的字符串操作方法。
// 判断字符串是否为空
boolean isEmpty = StrUtil.isEmpty(""); // true// 格式化字符串
String template = "姓名:{},年龄:{}";
String formatted = StrUtil.format(template, "张三", 25); // 姓名:张三,年龄:25// 截取字符串
String subStr = StrUtil.sub("Hello World", 0, 5); // "Hello"
日期时间工具(DateUtil)
DateUtil
模块简化了日期时间的操作,支持日期解析、格式化、计算等功能。
// 字符串转日期
Date date = DateUtil.parse("2023-10-01");// 日期加减
Date newDate = DateUtil.offsetDay(date, 1); // 加1天// 格式化日期
String formattedDate = DateUtil.format(date, "yyyy/MM/dd"); // "2023/10/01"
文件IO工具(FileUtil)
FileUtil
模块封装了常见的文件操作,如读写、复制、删除等。
// 读取文件内容
String content = FileUtil.readUtf8String("test.txt");// 写入文件
FileUtil.writeUtf8String("Hello Hutool", "output.txt");// 复制文件
FileUtil.copy("source.txt", "target.txt", true);
HTTP客户端工具(HttpUtil)
HttpUtil
模块提供了简洁的HTTP请求方法,支持GET、POST等请求方式。
// GET请求
String response = HttpUtil.get("https://example.com");// POST请求
Map<String, Object> params = new HashMap<>();
params.put("name", "Hutool");
String postResponse = HttpUtil.post("https://example.com/api", params);
加密解密工具(SecureUtil)
SecureUtil
模块支持多种加密算法,如MD5、SHA-1、AES等。
// MD5加密
String md5 = SecureUtil.md5("123456");// AES加密解密
String data = "Hello Hutool";
String key = "1234567812345678";
String encrypted = SecureUtil.aes(key.getBytes()).encryptHex(data);
String decrypted = SecureUtil.aes(key.getBytes()).decryptStr(encrypted);
集合工具(CollUtil)
CollUtil
模块提供了集合操作的便捷方法,如判空、分组、过滤等。
List<String> list = Arrays.asList("a", "b", "c");// 集合判空
boolean isNotEmpty = CollUtil.isNotEmpty(list); // true// 分组
Map<Character, List<String>> grouped = CollUtil.groupByField(list, str -> str.charAt(0));
反射工具(ReflectUtil)
ReflectUtil
模块简化了Java反射操作,支持动态调用方法、访问字段等。
// 调用方法
class TestClass {public void print(String msg) {System.out.println(msg);}
}
ReflectUtil.invoke(new TestClass(), "print", "Hello Reflection");// 访问字段
class TestField {private String name = "Hutool";
}
String name = (String) ReflectUtil.getFieldValue(new TestField(), "name");
验证码工具(CaptchaUtil)
CaptchaUtil
模块可用于生成图形验证码,支持干扰线、扭曲等效果。
// 生成验证码
LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
String code = captcha.getCode(); // 验证码文本
captcha.write("captcha.png"); // 保存验证码图片
BeanUtil:JavaBean操作工具
对象属性拷贝
将源对象的属性值拷贝到目标对象:
User srcUser = new User("张三", 25);
User targetUser = new User();
BeanUtil.copyProperties(srcUser, targetUser);
Map转JavaBean
将Map转换为JavaBean对象:
Map<String, Object> map = new HashMap<>();
map.put("name", "李四");
map.put("age", 30);
User user = BeanUtil.mapToBean(map, User.class, false);
复制插入
JavaBean转Map
将JavaBean对象转为Map:
User user = new User("王五", 28);
Map<String, Object> map = BeanUtil.beanToMap(user);
IdUtil:唯一ID生成工具
生成UUID
生成不带连字符的UUID
String uuid = IdUtil.simpleUUID();
生成Snowflake ID
基于Snowflake算法生成分布式ID(雪花算法)
long snowflakeId = IdUtil.getSnowflake().nextId();
生成ObjectId
MongoDB风格的ObjectId:
String objectId = IdUtil.objectId();
RandomUtil:随机数生成工具
生成随机整数
生成指定范围内的随机整数:
int randomInt = RandomUtil.randomInt(1, 100);
生成随机字符串
生成指定长度的随机字母数字字符串(可用在随机生成验证码)
String randomString = RandomUtil.randomString(10);
生成随机UUID
生成带连字符的标准UUID
String randomUUID = RandomUtil.randomUUID();
ZipUtil:压缩解压工具
压缩文件/目录
将文件或目录压缩为ZIP:
File file = new File("/path/to/file.txt");
File zipFile = ZipUtil.zip(file);
解压ZIP文件
解压ZIP到指定目录:
File zipFile = new File("/path/to/archive.zip");
File destDir = new File("/path/to/output");
ZipUtil.unzip(zipFile, destDir);
压缩到流
将文件压缩为ZIP并输出到:
File file = new File("/path/to/file.txt");
OutputStream out = new FileOutputStream("/path/to/output.zip");
ZipUtil.toZip(file, out);
以上方法均基于Hutool工具包的最新版本(建议使用5.8.x+)。具体使用时需确保引入依赖:
xml复制插入
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.20</version>
</dependency>