Java常用工具类
提供参数,时间,加密、验证码、邮件、跨域、随机数、Id生成等开发中常用到的工具。。。
一、导入依赖
<dependency>
<groupId>cn.gjing</groupId>
<artifactId>tools-common</artifactId>
<version>1.2.3</version>
</dependency>
使用须知
如果是Spring环境,确保一些工具的可用, 需要手动在xml文件中进行如下配置,SpringBoot环境无需配置
<bean id="xxx" class="cn.gjing.handle.ToolsCommonAdapter"/>
二、常用注解:
1、@NotNull
方法参数校验,如若要排除方法中的某个参数,搭配使用@Exclude注解到指定参数上;
2、@NotEmpty
方法参数校验, 可对null和空字符串进行校验, 如果要排除某些参数, 搭配使用@Exclude2注解到指定参数上, 如果需要自定义异常提示信息, 可设置message
3、@EnableCors
开启允许跨域,在启动类或者任意类使用该注解即可,会走默认配置,也可以自行配置,配置示例如下:
- yml方式
cors:
# 支持的方法类型
allowed-methods: POST,GET,DELETE,PUT...
# 支持的请求头
allowed-headers: xxx
# 支持的域名
allowed-origins: xxx
# 方法路径
path: /**
max-age: 1800
- javaBean方式
/**
* @author Gjing
**/
@Configuration
public class CorsConfiguration {
@Bean
public Cors cors() {
return Cors.builder()
.allowCredentials(Boolean.TRUE)
.maxAge(1800L)
.path("/**")
.build();
}
}
三、返回结果模板
1、ResultVO
通用返回结果模板,包含code(状态码)和message(提示信息)以及data(数据)
ResultVO<T> resultVo = ResultVO.success();
2、PageResult
分页查询返回结果集,包含data(数据)、totalPages(总页数)、CurrentPage(当前页数)、totalRows(总条数)、pageRows(每页的条数)
PageResult<T> pageResult = PageResult.of("data", 1);
3、ErrorResult
错误返回模板, 里面包含failure(状态码400时使用,包含code和message, code用于进一步确定错误), error(服务器型异常,一般用于500等, 只包含message)
ErrorResult.error(HttpStatus.BAD_REQUEST.getMsg());
四、参数校验工具类:
主要提供参数校验、处理,匹配等等, 使用时通过ParamUtil.xxx()使用,以下为该工具的所有方法介绍 :
- isEmpty:判断给定参数是否为空,可以是字符串、包装类型、数组、集合等
boolean isEmpty(T str)
- isNotEmpty:判断给定是否不为空,可以是字符串、包装类型、数组、集合等。。
boolean isNotEmpty(T str)
- requireNotNull:该参数不能为空,为空抛出NPE,否则返回原值
T requireNotNull(T str)
- ListHasEmpty:判断集合里是否含有空值
boolean ListHasEmpty(Collection<? extends T> list)
- multiEmpty:检查多参数里面是否有空值
boolean multiEmpty(Object...params)
- equals:判断两个参数是否相等
boolean equals(Object t, Object u)
- trim:去除字符串的空格
String trim(String str)
- trim:去除集合中的空元素
List<String> trim(List<String> list)
- removeSymbol:移除字符串两边的符号
String removeSymbol(String str, String symbol)
- removeStartSymbol:移除字符串开始的符号
String removeStartSymbol(String str, String symbol)
- removeEndSymbol:移除字符串末尾的符号
String removeEndSymbol(String str, String symbol)
- split:根据符号截取
String[] split(String str, String symbol)
- removeAllSymbol:移除字符串里的符号
String removeAllSymbol(String str, String symbol)
- contains:判断数组里是否包含指定的值
boolean contains(Object[] t, Object u)
- isEmail:判断是否为email
boolean isEmail(String email)
- isMobileNumber:判断是否是手机号码
boolean isMobileNumber(String phone)
- isTelPhone:判断是不是电话号码
boolean isTelPhone(String tel)
- 判断是否为邮编
boolean isPostCode(String postCode)
五、时间工具类:
对时间进行操作,使用时通过TimeUtil.xxx()调用,该工具的所有方法介绍如下 :
- dateToString:获取文本格式时间
String dateToString(Date date)
- dateToLocalDateTime:date转localDateTime
LocalDateTime dateToLocalDateTime(Date date)
- dateToLocalDate:date转localDate
LocalDate dateToLocalDate(Date date)
- localDateToDate:localDate转Date
Date localDateToDate(LocalDate localDate)
- LocalDateToString:LocalDate转指定格式字符串
String localDateToString(LocalDate localDate)
- LocalDateTimeToString:LocalDateTime转指定格式字符串
localDateTimeToString(LocalDateTime localDateTime)
- localTimeToString:LocalTime转指定格式字符串
localTimeToString(LocalTime localTime)
- stringToLocalDate:字符串日期转指定格式
LocalDate stringToLocalDate(String time)
- stringToLocalDateTime:字符串日期转指定格式
LocalDate stringToLocalDateTime(String time)
- localDateTimeToDate:LocalDateTime转
Date localDateTimeToDate(LocalDateTime dateTime)
- localDateTimeToStamp:localDateTime转时间
long localDateTimeToStamp(LocalDateTime localDateTime)
- stampToLocalDateTime:时间戳转
LocalDateTime stampToLocalDateTime(Long stamp)
- getYearsByStartTime:查询一个日期(年月日)到现在过了多少
Integer getYearsByStartTime(String startTime)
- dateToString:Date转字符串
String dateToString(Date date, String format)
- stringToDate:字符串转Date
Date stringToDate(String date)
- getDate:字符串日期转指定格式Date
Date stringToDate(String date, string format)
- stringDateToCalendar:字符串时间转日期
Calendar stringDateToCalendar(String str)
- calendarToDate:日期转Date
Date calendarToDate(Calendar calendar, String format)
- calendarToStringDate:日期转字符串
String calendarToStringDate(Calendar calendar, String format)
- getAllDaysOfMonth:获取某个时间所在月份的天数
int getAllDaysOfMonth(Date date)
- getDays:获取时间的天数,如2017-12-13,返回13
int getDays(Date date)
- getYears:获取时间所在的年份
int getYears(Date date)
- getMonth:获取时间所在月份
int getMonth(Date date)
- addMonth:增加月份
Date addMonth(Date date, int n)
- addDay:增加天数
Date addDay(Date date, int n)
- stringDateToStamp:字符串日期转时间戳
Long stringDateToStamp(String stringDate)
- stampToStringDate:时间戳转字符串
String stampToStringDate(Long timeStamp)
- dateBetween:计算两个日期相差的天数(不包括今天)
int dateBetween(String startDate, String endDate)
- dateBetweenIncludeToday:计算两个日期相差的天数(包括今天)
int dateBetween(String startDate, String endDate)
六、加密工具类
主要用于数据加密,使用时通过EncryptionUtil.xxx()调用,该工具包含的所有方法如下:
- of:生成加密实例
EncryptionUtil of()
- encodeMd5:MD5加密
String encodeMd5(String body)
- encodeBase64:Base64加密
encodeBase64(String content)
- decodeBase64:Base64解密
String decodeBase64(String content)
- encodeSha256Hmac:sha566 hmac加密
String encodeSha256Hmac(String str, String secret)
- sha1Hmac:sha1Hmac加密
String sha1Hmac(String str, String secret)
- encodeAes:AES加密
String encodeAes(String content, String password)
- decodeAes:AES解密
String decodeAes(String content, String password)
七、随机数工具类
用于随机生成数字或字符串,使用时通过RandomUtil.xxx()调用,该工具包含的所有方法如下 :
- randomInt:获取随机整数,可设置最大值和最小值
int randomInt(int min, int max)
- getRandom:获取一个Random实例
Random getRandom()
- generateMixString:生成混合指定长度字符串(数字、字母大小写)
String generateMixString(int length)
- generateString:获取指定长度纯字符串(字母大小写)
String generateString(int length)
- generateNumber:获取指定长度数字字符串
String generateNumber(int length)
八、Bean工具类:
使用时通过BeanUtil.xxx()调用
1、copyProperties
属性复制,用于将一个对象的属性赋值到另一个对象,两个对象间的参数名和数据类型必须相同
参数说明
| 参数 | 描述 |
|---|---|
| source | 源对象 |
| target | 目标对象 |
| ignores | 忽略的字段,设置后不会进行复制 |
2、toBean
将map转为bean对象
参数说明
| 参数 | 描述 |
|---|---|
| map | 需要转为Bean的map |
| beanClass | 目标Bean的class |
3、findMethod
查找类中的方法
参数说明
| 参数 | 描述 |
|---|---|
| clazz | 目标类class |
| methodName | 方法名 |
| paramTypes | 方法参数类型 |
4、findDeclaredMethod
查找类中声明的方法
参数说明
| 参数 | 描述 |
|---|---|
| clazz | 目标类class |
| methodName | 方法名 |
| paramTypes | 方法参数类型 |
5、setFieldValue
给类中的某个字段设置值
参数说明
| 参数 | 描述 |
|---|---|
| o | 字段所在的对象 |
| field | 字段 |
| value | 值 |
6、getFieldValue
获取某个字段的值
参数说明
| 参数 | 描述 |
|---|---|
| o | 字段所在的对象 |
| field | 字段 |
7、toMap
将bean对象转为map
参数说明
| 参数 | 描述 |
|---|---|
| bean | 需要转为map的bean对象 |
九、验证码工具类
用于生成英文和数字混合的验证码,使用时通过构造AuthCodeUtil在进行调用其中的方法,构造时参数如下 :
| 参数 | 描述 |
|---|---|
| width | 验证码图片宽度 |
| height | 验证码图片高度 |
| codeCount | 验证码字符个数 |
| lineCount | 验证码干扰线数 |
该工具下的所有方法如下:
1、writeToLocal
生成验证码到本地,案例如下
public static void main(String[] args) {
AuthCodeUtil authCodeUtil = new AuthCodeUtil(160,40,5,150);
try {
String path="/文件夹路径/code.png";
//写入到本地时可以通过getCode()方法获取生成的验证码
String code = authCodeUtil.writeToLocal(path).getCode();
System.out.println(code);
} catch (IOException e) {
e.printStackTrace();
}
}
2、getCode
获取生成的验证码字符
String code = authCodeUtil.writeToLocal(xxx).getCode;
3、write
以流的方式返回给前端,案例如下
@GetMapping("/code")
public void getCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
AuthCodeUtil authCodeUtil = new AuthCodeUtil(100, 50, 4, 50);
response.setContentType("image/jpeg");
//禁止图像缓存
response.setHeader("param", "no-cache");
response.setDateHeader("Expires", 0);
authCodeUtil.write(response.getOutputStream());
}
十、Id生成工具类
生成无符号的UUID和通过雪花算法生成一个唯一ID,在项目使用时,先通过@Resource注入,然后通过idUtil.xxx()生成,该工具包含的方法如下 :
1、uuid
获取去除-符号的uuid
idUtil.uuid();
2、snowId
得到一个唯一的ID,在多服务需要操作同一个数据表的情况下, 需要保证每个服务的centerId和machineId唯一
idUtil.snowId();
配置如下
snow:
center-id: 数据中心id, 范围0-31, 默认0
machine-id: 机器id, 范围0-31, 默认0
十一、文件工具类
对文件的一些操作,使用时通过FileUtil.of()生成实例之后再调用其中的方法,包含的所有方法如下 :
1、downloadByUrl
从远程URL地址下载文件到本地
FileUtil.of().downloadByUrl(fileUrl, fileName, savePath);
参数说明
| 参数 | 描述 |
|---|---|
| fileUrl | 要下载的文件URL地址 |
| fileName | 保存到本地的文件名, 需要加后缀 |
| savePath | 保存到本地的目录 |
2、downloadByStream
将本地的指定地址文件通过流下载
FileUtil.of().downlocdByStream(response, file);
参数说明
| 参数 | 描述 |
|---|---|
| response | HttpServletResponse |
| file | 文件对象 |
3、getBytes
将指定路径下的文件转为byte数组
byte[] data = FileUtil.of().getBytes(file);
4、writeFile
将字节数组写入到指定文件, 写入成功返回true
boolean b = FileUtil.of().writeFile(bytes, file);
参数说明
| 参数 | 描述 |
|---|---|
| bytes | byte数组 |
| file | 文件对象 |
5、readInputStream
从输入流读取内容并返回字节数组
byte[] b = FileUtil.of().readInputStream(inputStream);
6、getExtension
获取文件扩展名
String name = FileUtil.of().getExtension(fileName);
十二、邮件工具类
用于发送邮件,支持普通邮件和带附件邮件,支持html格式文本,支持群发和抄送,返回true为发送成功,使用时通过EmailUtil.of()生成实例之后在进行其中的方法,of()方法参数如下 :
| 参数 | 描述 |
|---|---|
| host | smtp服务器地址,比如qq邮箱:smtp.qq.com |
| password | 发送者邮箱密码,有些邮箱需要用授权码代替密码 |
| from | 发送人邮箱 |
该工具包含的所有方法如下 :
1、sendEmail
发送邮件,参数如下:
| 参数 | 描述 |
|---|---|
| subject | 主题 |
| body | 邮件内容,支持HTML |
| files | 要发送的附件物理地址,不要可以传null或者空数组 |
| tos | 收件人邮箱账号,多个使用逗号隔开 |
| copyTo | 抄送人地址,多个用逗号隔开,不抄送可以传null或者空字符串 |
完整示例如下 :
public static void main(String[] args) {
boolean b = EmailUtil.of("smtp.qq.com", "发送人密码或者授权码", "发送人邮箱")
.sendEmail("主题", "内容",new String[]{"附件物理地址"},"收件人邮箱地址", "抄送人邮箱地址");
if (b) {
System.out.println("发送成功");
}
}
工具源码地址:tools-common