每日技术干货,第一时间送达!
-
Github:https://github.com/dromara/hutool -
Gitee:https://gitee.com/chinabugotech/hutool
-
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 扩展模块,对第三方封装(模板引擎、邮件等)
-
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)封装实现
hutool-ai AI大模型封装实现
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.40</version>
</dependency>
// 快速对话
String chat = AIUtil.chat(new AIConfigBuilder(ModelName.DEEPSEEK.getValue()).setApiKey("your key").build(), "写一首赞美我的诗");
//多轮对话
List<Message> messages = new ArrayList<>();
messages.add(new Message("system","你是财神爷,只会说“我是财神”"));
messages.add(new Message("user","你是谁啊?"));
String chat = AIUtil.chat(new AIConfigBuilder(ModelName.DEEPSEEK.getValue()).setApiKey("your key").build(), messages);
我们知道,JDK中的Cloneable接口只是一个空接口,并没有定义成员,它存在的意义仅仅是指明一个类的实例化对象支持位复制(就是对象克隆),如果不实现这个类,调用对象的clone()方法就会抛出CloneNotSupportedException异常。而且,因为clone()方法在Object对象中,返回值也是Object对象,因此克隆后我们需要自己强转下类型。
因此,cn.hutool.core.clone.Cloneable接口应运而生。此接口定义了一个返回泛型的成员方法,这样,实现此接口后会提示必须实现一个public的clone方法,调用父类clone方法即可:
/**
* 猫猫类,使用实现Cloneable方式
* @author Looly
*
*/
privatestaticclassCatimplementsCloneable<Cat>{
private String name = "miaomiao";
privateint age = 2;
@Override
public Cat clone(){
try {
return (Cat) super.clone();
} catch (CloneNotSupportedException e) {
thrownew CloneRuntimeException(e);
}
}
}
cc.ryanc.halo.web.controller.admin.BackupController.backupResources = 0 01 * * ?
cc.ryanc.halo.web.controller.admin.BackupController.backupDatabase = 001 * * ?
cc.ryanc.halo.web.controller.admin.BackupController.backupPosts = 001 * * ?
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
this.loadActiveTheme();
this.loadOptions();
this.loadFiles();
this.loadThemes();
//启动定时任务
CronUtil.start();
log.info("定时任务启动成功!");
}
//转换为字符串
int a =1;
String aStr = Convert.toStr(a);
//转换为指定类型数组
String[] b = {"1","2","3","4"};
Integer[] bArr = Convert.toIntArray(b);
//转换为日期对象
String dateStr ="2017-05-06";
Date date = Convert.toDate(dateStr);
//转换为列表
String[] strArr = {"a","b","c","d"};
List<String> strList = Convert.toList(String.class, strArr);
//Date、long、Calendar之间的相互转换
//当前时间
Date date = DateUtil.date();
//Calendar转Date
date = DateUtil.date(Calendar.getInstance());
//时间戳转Date
date = DateUtil.date(System.currentTimeMillis());
//自动识别格式转换
String dateStr ="2017-03-01";
date = DateUtil.parse(dateStr);
//自定义格式化转换
date = DateUtil.parse(dateStr,"yyyy-MM-dd");
//格式化输出日期
String format = DateUtil.format(date,"yyyy-MM-dd");
//获得年的部分
int year = DateUtil.year(date);
//获得月份,从0开始计数
int month = DateUtil.month(date);
//获取某天的开始、结束时间
Date beginOfDay = DateUtil.beginOfDay(date);
Date endOfDay = DateUtil.endOfDay(date);
//计算偏移后的日期时间
Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH,2);
//计算日期时间之间的偏移量
long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);
//判断是否为空字符串
String str = "test";
StrUtil.isEmpty(str);
StrUtil.isNotEmpty(str);
//去除字符串的前后缀
StrUtil.removeSuffix("a.jpg", ".jpg");
StrUtil.removePrefix("a.jpg", "a.");
//格式化字符串
String template = "这只是个占位符:{}";
String str2 = StrUtil.format(template, "我是占位符");
LOGGER.info("/strUtil format:{}", str2);
//获取定义在src/main/resources文件夹中的配置文件
ClassPathResource resource =new ClassPathResource("generator.properties");
Properties properties =new Properties();
properties.load(resource.getStream());
LOGGER.info("/classPath:{}", properties);
ReflectUtil
Java反射工具类,可用于反射获取类的方法及创建对象。
//获取某个类的所有方法
Method[] methods = ReflectUtil.getMethods(PmsBrand.class);
//获取某个类的指定方法
Method method = ReflectUtil.getMethod(PmsBrand.class,"getId");
//使用反射来创建对象
PmsBrand pmsBrand = ReflectUtil.newInstance(PmsBrand.class);
//反射执行对象的方法
ReflectUtil.invoke(pmsBrand,"setId",1);
double n1 = 1.234;
double n2 =1.234;
double result;
//对float、double、BigDecimal做加减乘除操作
result = NumberUtil.add(n1, n2);
result = NumberUtil.sub(n1, n2);
result = NumberUtil.mul(n1, n2);
result = NumberUtil.div(n1, n2);
//保留两位小数
BigDecimal roundNum = NumberUtil.round(n1,2);
String n3 ="1.234";
//判断是否为数字、整数、浮点数
NumberUtil.isNumber(n3);
NumberUtil.isInteger(n3);
NumberUtil.isDouble(n3);
BeanUtil
JavaBean的工具类,可用于Map与JavaBean对象的互相转换以及对象属性的拷贝。
PmsBrand brand =new PmsBrand();
brand.setId(1L);
brand.setName("小米");
brand.setShowStatus(0);
//Bean转Map
Map<String, Object>map = BeanUtil.beanToMap(brand);
LOGGER.info("beanUtil bean to map:{}",map);
//Map转Bean
PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class,false);
LOGGER.info("beanUtil map to bean:{}", mapBrand);
//Bean属性拷贝
PmsBrand copyBrand =new PmsBrand();
BeanUtil.copyProperties(brand, copyBrand);
LOGGER.info("beanUtil copy properties:{}", copyBrand);
//数组转换为列表
String[] array =new String[]{"a","b","c","d","e"};
List<String> list = CollUtil.newArrayList(array);
//join:数组转字符串时添加连接符号
String joinStr = CollUtil.join(list,",");
LOGGER.info("collUtil join:{}", joinStr);
//将以连接符号分隔的字符串再转换为列表
List<String> splitList = StrUtil.split(joinStr,',');
LOGGER.info("collUtil split:{}", splitList);
//创建新的Map、Set、List
HashMap<Object,Object> newMap = CollUtil.newHashMap();
HashSet<Object> newHashSet = CollUtil.newHashSet();
ArrayList<Object> newList = CollUtil.newArrayList();
//判断列表是否为空
CollUtil.isEmpty(list);
//将多个键值对加入到Map中
Map<Object,Object> map = MapUtil.of(newString[][]{
{"key1","value1"},
{"key2","value2"},
{"key3","value3"}
});
//判断Map是否为空
MapUtil.isEmpty(map);
MapUtil.isNotEmpty(map);
AnnotationUtil
注解工具类,可用于获取注解与注解中指定的值。
//获取指定类、方法、字段、构造器上的注解列表
Annotation[] annotationList = AnnotationUtil.getAnnotations(HutoolController.class,false);
LOGGER.info("annotationUtil annotations:{}", annotationList);
//获取指定类型注解
Api api = AnnotationUtil.getAnnotation(HutoolController.class, Api.class);
LOGGER.info("annotationUtil api value:{}", api.description());
//获取指定类型注解的值
Object annotationValue = AnnotationUtil.getAnnotationValue(HutoolController.class, RequestMapping.class);
//MD5加密
String str ="123456";
String md5Str = SecureUtil.md5(str);
LOGGER.info("secureUtil md5:{}", md5Str);
CaptchaUtil
验证码工具类,可用于生成图形验证码。
//生成验证码图片
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200,100);
try {
request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
response.setContentType("image/png");//告诉浏览器输出内容为图片
response.setHeader("Pragma","No-cache");//禁止浏览器缓存
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expire",0);
lineCaptcha.write(response.getOutputStream());
}catch (IOException e) {
e.printStackTrace();
}
往期推荐
8年开发,连登陆接口都写这么烂...
全球AI产品50强!国产 AI 居然占了这么多?你跟上了吗?
SpringBoot 方法级耗时监控器
13 秒插入 30 万条数据,这才是批量插入正确的姿势!
7款颜值当道的 Linux 系统
IDEA 源码阅读利器,你居然还不会?

