关于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语言使用slices包轻松实现排序功能

《Go语言使用slices包轻松实现排序功能》在Go语言开发中,对数据进行排序是常见的需求,Go1.18版本引入的slices包提供了简洁高效的排序解决方案,支持内置类型和用户自定义类型的排序操作,本... 目录一、内置类型排序:字符串与整数的应用1. 字符串切片排序2. 整数切片排序二、检查切片排序状态:

CentOS7增加Swap空间的两种方法

《CentOS7增加Swap空间的两种方法》当服务器物理内存不足时,增加Swap空间可以作为虚拟内存使用,帮助系统处理内存压力,本文给大家介绍了CentOS7增加Swap空间的两种方法:创建新的Swa... 目录在Centos 7上增加Swap空间的方法方法一:创建新的Swap文件(推荐)方法二:调整Sww

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW