关于Hutool的模块使用说明方法

2024-06-19 06:28

本文主要是介绍关于Hutool的模块使用说明方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

Hutool包含多个模块,每个模块针对特定的功能需求提供支持:• hutool-aop:JDK动态代理封装,为非IOC环境提供切面支持。• hutool-bloomFilter:布隆过滤器,提供基于Hash算法的实现。• hutool-cache:简单的缓存实现。• hutool-core:核心模块,包含Bean操作、日期处理等多种工具。• hutool-cron:定时任务模块,支持类似Crontab的表达式。• hutool-crypto:加密解密模块,封装了对称、非对称及摘要算法。• hutool-db:基于JDBC的数据操作封装,采用ActiveRecord思想。• hutool-dfa:基于DFA模型的多关键字查找。• hutool-extra:扩展模块,封装第三方服务如模板引擎、邮件、Servlet等。• hutool-http:基于HttpUrlConnectionHttpClient封装。• hutool-log:自动识别日志实现的日志门面。• hutool-script:脚本执行封装,如Javascript。• hutool-setting:功能强大的配置文件和Properties封装。• hutool-system:系统参数调用封装,如JVM信息。• hutool-json:JSON实现。• hutool-captcha:图片验证码实现。• hutool-poi:针对POIExcelWord封装。• hutool-socket:基于Java NIOAIOSocket封装。• hutool-jwt:JSON Web TokenJWT)封装。可以根据需求对每个模块单独引入,也可以通过引入hutool-all方式引入所有模块。

Hutool的使用,在Maven项目中,可以通过添加以下依赖来引入Hutool:

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.28</version>
</dependency>

HTTP请求示例
Hutool简化了HTTP请求的处理,以下是发送GET和POST请求的示例:

public static void main(String[] args) {// GET请求示例String content = HttpUtil.get("https://www.example.com/api/data");// POST请求示例HashMap<String, Object> param = new HashMap<>();param.put("city", "重庆");String result = HttpUtil.post("https://www.example.com/api/post", param);System.out.println("GET请求返回:" + content);System.out.println("POST请求返回:" + result);
}

随机验证码生成方法

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.crypto.SecureUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
/*** 系统基础信息--图片验证码管理模块* @author weimeilayer@gmail.com* @date 2021年3月17日 图片验证码(支持算术形式)*/
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/code")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Tag(description = "图片验证码模块操作接口", name = "图片验证码模块操作接口")
public class SysCaptchaController {/*** 宽*/private final Integer WIDTH = 120;/*** 高*/private final Integer HEIGHT = 40;/*** 编码长度*/private final Integer CODE_COUNT = 4;/*** 干扰线数*/private final Integer LINE_COUNT = 20;/*** 验证码长度*/private final Integer CODE_SIZE = 4;/*** RedisTemplate*/private final RedisTemplate redisTemplate;/*** 验证码有效期*/private final Integer CODE_TIME = 60;/*** 验证码前缀*/private final String DEFAULT_CODE_KEY = "DEFAULT_CODE_KEY:";/*** 验证码* * @param key* @return*/@Operation(summary = "生成字节流验证码")@GetMapping(value = "/captcha/{key}")public R randomImage(@PathVariable String key) {// 第一种:验证码纯数字化RandomGenerator randomGenerator = new RandomGenerator("0123456789", CODE_SIZE );LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(WIDTH, HEIGHT, CODE_COUNT, LINE_COUNT);// 调用父类的 setGenerator() 方法,设置验证码的类型lineCaptcha.setGenerator(randomGenerator);String code = lineCaptcha.getCode();String realKey = SecureUtil.md5(code + key);redisTemplate.opsForValue().set(DEFAULT_CODE_KEY  + realKey, code, CODE_TIME,TimeUnit.SECONDS);Map<String, String> res = new HashMap<>(2);res.put("realKey", realKey);res.put("img", lineCaptcha.getImageBase64Data());//第二种:验证码含字母/*LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(WIDTH, HEIGHT, CODE_COUNT, LINE_COUNT);String code = lineCaptcha.getCode();String realKey = SecureUtil.md5(code + key);if (GlobalConfig.isRedisSwitch()) {redisTemplate.opsForValue().set(DEFAULT_CODE_KEY   + realKey, code, CODE_TIME, TimeUnit.SECONDS);}Map<String, String> res = new HashMap<>(2);res.put("realKey", realKey);res.put("img", lineCaptcha.getImageBase64());*/return R.ok(res);}

拼音工具

导入
import cn.hutool.extra.pinyin.PinyinUtil;// 获取全部拼音 输出结果:ni hao
String pinyin = PinyinUtil.getPinyin("你好", " ");
// 获取拼音首字母 输出结果:h s d y g
String result = PinyinUtil.getFirstLetter("H是第一个", ", ");

数字处理工具,包括保留小数、时间格式化、校验数字和生成随机数

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.NumberUtil;/*** @author weimeilayer@gmail.com ✨* @date 💓💕 2023年5月20日 🐬🐇 💓💕*/
public class NumberUtilExample {public static void main(String[] args) {double number1 = 1234.56789;double number2 = 9876.54321;// 保留小数点后两位,并以字符串形式返回String rounded1 = NumberUtil.roundStr(number1, 2);String rounded2 = NumberUtil.roundStr(number2, 2);Console.log("保留小数点后两位:");Console.log(rounded1); // 输出: 1234.57Console.log(rounded2); // 输出: 9876.54// 保留小数点后四位,并返回double类型BigDecimal rounded3 = NumberUtil.round(number1, 4);BigDecimal rounded4 = NumberUtil.round(number2, 4);Console.log("保留小数点后四位:");Console.log(rounded3); // 输出: 1234.5679Console.log(rounded4); // 输出: 9876.5432}
}

数据脱敏
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.DesensitizedUtil;

/*** @author weimeilayer@gmail.com ✨* @date 💓💕 2024年3月5日 🐬🐇 💓💕*/public class DesensitizedUtilExample {public static void main(String[] args) {// 身份证号脱敏String idCard = "222222202205201314";String desensitizedIdCard = DesensitizedUtil.idCardNum(idCard, 1, 2);Console.log("原始身份证号: " + idCard);Console.log("脱敏后身份证号: " + desensitizedIdCard); // 输出: 2***************14// 手机号脱敏String phoneNumber = "13800001314";String desensitizedPhone = DesensitizedUtil.mobilePhone(phoneNumber);Console.log("原始手机号: " + phoneNumber);Console.log("脱敏后手机号: " + desensitizedPhone); // 输出: 138****1314// 密码脱敏String password = "1234567890";String desensitizedPassword = DesensitizedUtil.password(password);Console.log("原始密码: " + password);Console.log("脱敏后密码: " + desensitizedPassword); // 输出: **********}
}

布隆过滤器

import cn.hutool.bloomfilter.BitMapBloomFilter;
import cn.hutool.core.lang.Console;/*** @author weimeilayer@gmail.com ✨* @date 💓💕 2024年3月5日 🐬🐇 💓💕*/
public class BloomFilterExample {public static void main(String[] args) {// 初始化布隆过滤器,预期容量为5个城市名BitMapBloomFilter filter = new BitMapBloomFilter(5);// 添加城市名filter.add("北京");filter.add("上海");filter.add("广州");// 查找城市名boolean exists1 = filter.contains("北京");boolean exists2 = filter.contains("深圳");Console.log("布隆过滤器测试:");Console.log("是否存在 '北京': " + exists1); // 输出: trueConsole.log("是否存在 '深圳': " + exists2); // 输出: false}
}

Hutool的MailUtil简化了邮件发送过程,以下是发送邮件的示例

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;/*** @author weimeilayer@gmail.com ✨* @date 💓💕 2024年3月5日 🐬🐇 💓💕*/
public class MailUtilExample {public static void main(String[] args) {// 发件人邮箱配置MailAccount mailAccount = new MailAccount();mailAccount.setHost("smtp.example.com");mailAccount.setPort(465); // SMTP端口mailAccount.setAuth(true);mailAccount.setFrom("sender@example.com");mailAccount.setUser("sender@example.com");mailAccount.setPass("password"); // 发件人邮箱密码或授权码// 收件人邮箱String to = "recipient@example.com";// 发送邮件MailUtil.send(mailAccount, to, "测试邮件", "这是一封来自Hutool的测试邮件。");}
}

这篇关于关于Hutool的模块使用说明方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1074257

相关文章

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优