[update] 1.5.2-jre11 发布

This commit is contained in:
its 2024-04-08 16:26:04 +08:00
parent 8e8f390b5d
commit 59f0e6e296
18 changed files with 400 additions and 334 deletions

View File

@ -7,7 +7,8 @@ yexuejc-base 更新记录
**update** <br/>
1. 升级相关依赖
2. 依赖工具读取文件[FileInput.java](src/main/java/com/yexuejc/base/file/FileInput.java)从[FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)中提取出来
3. 优化[FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)
4. 优化[JwtUtil.java](src/main/java/com/yexuejc/base/util/JwtUtil.java)
---

View File

@ -1,6 +1,11 @@
package com.yexuejc.base.annotation;
import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 设置csv header

View File

@ -1,16 +1,16 @@
package com.yexuejc.base.converter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.yexuejc.base.constant.DateConsts;
import com.yexuejc.base.util.StrUtil;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
/**
* json转LocalDate
*

View File

@ -1,13 +1,13 @@
package com.yexuejc.base.converter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* localDate转json
*

View File

@ -1,16 +1,16 @@
package com.yexuejc.base.converter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.yexuejc.base.constant.DateConsts;
import com.yexuejc.base.util.StrUtil;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
/**
* json转LocalDateTime
*

View File

@ -1,11 +1,11 @@
package com.yexuejc.base.converter;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
/**
* <pre>
* json中的转String对象时值为null

View File

@ -1,17 +1,17 @@
package com.yexuejc.base.converter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.yexuejc.base.constant.DateConsts;
import com.yexuejc.base.util.StrUtil;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.yexuejc.base.constant.DateConsts;
import com.yexuejc.base.util.StrUtil;
/**
* json转LocalDateTime
*

View File

@ -1,14 +1,14 @@
package com.yexuejc.base.converter;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.yexuejc.base.constant.DateConsts;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
/**
* Timestamp转json
*

View File

@ -50,7 +50,7 @@ public class CsvToBean {
return this;
}
public boolean isHasHeader() {
public boolean hasHeader() {
return hasHeader;
}

View File

@ -448,6 +448,8 @@ public class DateTimeUtil {
System.out.println(format(getYear4First().atTime(LocalTime.MIN)));
System.out.println(format(getYear4Last().atTime(LocalTime.MAX)));
System.out.println(parseLocalDateTime10(System.currentTimeMillis() / 1000));
System.out.println(parseLocalDateTime13(System.currentTimeMillis()));
}*/

View File

@ -59,8 +59,8 @@ public class DateUtil {
/**
* 比较两个日期大小
*
* @param date1
* @param date2
* @param date1 格式 yyyy-MM-dd
* @param date2 格式 yyyy-MM-dd
* @return date1>date2返回1;date1=date2返回0;date1<date2返回-1
* @throws ParseException
*/
@ -232,4 +232,19 @@ public class DateUtil {
return Date.from(date.toInstant().atZone(currentZone).withZoneSameInstant(targetZone).toInstant());
}
/* public static void main(String[] args) throws ParseException {
System.out.println(DateUtil.currentDate());
System.out.println(DateUtil.currentTime());
System.out.println(DateUtil.currentDateTime());
System.out.println(DateUtil.dateCompare("2024-01-25","2025-01-01"));
System.out.println(DateUtil.str2date("2024-01-25"));
System.out.println(DateUtil.date2str(new Date()));
System.out.println(DateUtil.str2dateTime("2024-04-08 11:01:39.361"));
System.out.println(DateUtil.dateTime2str(new Date()));
System.out.println(DateUtil.getCurrentWeek(1));
System.out.println(DateUtil.dateMinus(DateUtil.datePlus(new Date(),50),new Date(),'M'));
System.out.println(DateUtil.convertUTC(new Date()));
System.out.println(DateUtil.convertTimezone(new Date(),"UTC","Asia/Shanghai"));
System.out.println(DateUtil.convertTimezone(new Date(),"UTC"));
}*/
}

View File

@ -6,17 +6,19 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.CRC32;
@ -38,7 +40,7 @@ import io.jsonwebtoken.lang.Assert;
public class FileUtil {
static Logger logger = Logger.getLogger(FileUtil.class.getName());
public FileUtil() {
private FileUtil() {
}
private static final String TYPE_TAR_GZ = ".tar.gz";
@ -66,25 +68,41 @@ public class FileUtil {
return null;
}
/**
* 判断文件是否存在
*
* @param filePath
* @return false 文件不存在true 文件存在
*/
public static boolean isFileExist(String filePath) {
if (StrUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
return file.exists() && !file.isDirectory();
}
/**
* 判断文件是否存在,不存在就创建一个空的
*
* @param file
*/
public static void judeFileExists(File file) {
if (file.exists()) {
logger.severe("file exists");
} else {
logger.info("file not exists, create it ...");
try {
file.createNewFile();
} catch (IOException e) {
boolean b = file.createNewFile();
if (b) {
logger.info("file create success");
} else {
logger.severe("file create fail");
e.printStackTrace();
}
} catch (IOException e) {
logger.log(Level.WARNING, "file create fail", e);
}
}
}
/**
@ -121,72 +139,9 @@ public class FileUtil {
* @return
*/
public static String sha1(File file) {
FileInputStream in = null;
try {
in = new FileInputStream(file);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] buffer = new byte[1024 * 1024 * 10];
int len = 0;
while ((len = in.read(buffer)) > 0) {
digest.update(buffer, 0, len);
}
String sha1 = new BigInteger(1, digest.digest()).toString(16);
int length = 40 - sha1.length();
if (length > 0) {
for (int i = 0; i < length; i++) {
sha1 = "0" + sha1;
}
}
return sha1;
} catch (NoSuchAlgorithmException e) {
logger.severe("system algorithm error.");
e.printStackTrace();
} catch (FileNotFoundException e) {
logger.severe("file doesn't exist or is not a file");
e.printStackTrace();
} catch (IOException e) {
logger.severe("The operation file is an IO exception.");
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
logger.severe("close FileInputStream IO exception.");
}
}
return null;
return getDigest(file, "SHA-1");
}
/***
* 计算SHA1码
*
* @return String 适用于上G大的文件
* @throws NoSuchAlgorithmException
* */
public static String sha1ByBigFile(File file) {
MessageDigest messagedigest = null;
try {
messagedigest = MessageDigest.getInstance("SHA-1");
FileInputStream in = new FileInputStream(file);
FileChannel ch = in.getChannel();
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
messagedigest.update(byteBuffer);
return StrUtil.toHex(messagedigest.digest());
} catch (NoSuchAlgorithmException e) {
logger.severe("system algorithm error.");
e.printStackTrace();
} catch (FileNotFoundException e) {
logger.severe("file doesn't exist or is not a file");
e.printStackTrace();
} catch (IOException e) {
logger.severe("The operation file is an IO exception.");
e.printStackTrace();
}
return null;
}
/**
* 文件md5
@ -195,66 +150,40 @@ public class FileUtil {
* @return
*/
public static String md5(File file) {
FileInputStream in = null;
try {
in = new FileInputStream(file);
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024 * 1024 * 10];
int len = 0;
while ((len = in.read(buffer)) > 0) {
digest.update(buffer, 0, len);
}
String md5 = new BigInteger(1, digest.digest()).toString(16);
int length = 32 - md5.length();
if (length > 0) {
for (int i = 0; i < length; i++) {
md5 = "0" + md5;
}
}
return md5;
} catch (IOException e) {
logger.severe("The operation file is an IO exception.");
} catch (NoSuchAlgorithmException e) {
logger.severe("system algorithm error.");
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
logger.severe("close FileInputStream IO exception.");
}
}
return null;
return getDigest(file, "MD5");
}
/**
* 对一个文件获取md5
*
* @return md5串
* @throws NoSuchAlgorithmException
* 获取文件的散列值
* @param file
* @param digestCode
* @return
*/
public static String md5ByBigFile(File file) {
MessageDigest messagedigest = null;
try {
messagedigest = MessageDigest.getInstance("MD5");
FileInputStream in = new FileInputStream(file);
FileChannel ch = in.getChannel();
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
file.length());
messagedigest.update(byteBuffer);
return StrUtil.toHex(messagedigest.digest());
private static String getDigest(File file, String digestCode) {
try (FileInputStream in = new FileInputStream(file)) {
MessageDigest digest = MessageDigest.getInstance(digestCode);
FileChannel channel = in.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); // 1MB 缓冲区
// 读取文件内容并更新 MessageDigest
while (channel.read(buffer) != -1) {
buffer.flip(); // Buffer 从写模式切换到读模式
digest.update(buffer); // 更新 MessageDigest
buffer.clear(); // 清空 Buffer
}
// 计算最终的 SHA-1 散列值
byte[] sha1Bytes = digest.digest();
// 将字节数组转换为十六进制字符串
StringBuilder sha1Builder = new StringBuilder();
for (byte b : sha1Bytes) {
sha1Builder.append(String.format("%02x", b));
}
return sha1Builder.toString();
} catch (NoSuchAlgorithmException e) {
logger.severe("system algorithm error.");
e.printStackTrace();
logger.log(Level.SEVERE, "system algorithm error.", e);
} catch (FileNotFoundException e) {
logger.severe("file doesn't exist or is not a file");
e.printStackTrace();
logger.log(Level.SEVERE, "file doesn't exist or is not a file", e);
} catch (IOException e) {
logger.severe("The operation file is an IO exception.");
e.printStackTrace();
logger.log(Level.SEVERE, "The operation file is an IO exception.", e);
}
return null;
}
@ -262,37 +191,24 @@ public class FileUtil {
/**
* 获取文件CRC32码
*
* @return String
* @return 获取失败返回-1
*/
public static String crc32(File file) {
public static long crc32(File file) {
CRC32 crc32 = new CRC32();
// MessageDigest.get
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[8192];
try (FileInputStream fileInputStream = new FileInputStream(file);) {
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = fileInputStream.read(buffer)) != -1) {
crc32.update(buffer, 0, length);
}
return crc32.getValue() + "";
return crc32.getValue();
} catch (FileNotFoundException e) {
logger.severe("file doesn't exist or is not a file");
e.printStackTrace();
return null;
logger.log(Level.SEVERE, "file doesn't exist or is not a file", e);
return -1;
} catch (IOException e) {
logger.severe("The operation file is an IO exception.");
e.printStackTrace();
return null;
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
logger.severe("close FileInputStream IO exception.");
e.printStackTrace();
}
logger.log(Level.SEVERE, "The operation file is an IO exception.", e);
return -1;
}
}
@ -304,7 +220,7 @@ public class FileUtil {
*/
public static String base64ToStr(File file) {
try {
byte[] bytes = Files.readAllBytes(Path.of(file.getPath()));
byte[] bytes = Files.readAllBytes(Paths.get(file.getPath()));
return Base64.getEncoder().encodeToString(bytes);
} catch (IOException e) {
logger.severe("The operation file is an IO exception.");
@ -320,10 +236,10 @@ public class FileUtil {
* </i>
*
* @param decode {@link FileUtil#base64ToStr(File)} 的结果
* @param fileName 保存文件名称包含路径
* @param fileName 文件名称包含路径
* @return 返回保存地址
*/
public static String base64ToFile(String decode, String fileName) {
public static String base64ToFile(String decode, String fileName) throws IOException {
return base64ToFile(Base64.getDecoder().decode(decode.getBytes()), fileName);
}
@ -331,29 +247,16 @@ public class FileUtil {
* base64转文件
* <p>
* <i>
* 文件转base64请使用 {@link FileUtil#base64ToStr(File)}
* 文件转base64请使用 {@link FileUtil#base64ToStr(File)}}
* </i>
*
* @param decode baseByte
* @param fileName 文件名称包含路径
* @return 返回保存地址
*/
public static String base64ToFile(byte[] decode, String fileName) {
FileOutputStream out = null;
try {
out = new FileOutputStream(fileName);
public static String base64ToFile(byte[] decode, String fileName) throws IOException {
try (FileOutputStream out = new FileOutputStream(fileName)) {
out.write(decode);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName;
}
@ -361,50 +264,18 @@ public class FileUtil {
/**
* 获取文件大小 直接返回大小
*
* @param f
* @param path 文件地址
* @return f.length()
*/
public static long size(File f) {
if (f.exists() && f.isFile()) {
return f.length();
public static long size(Path path) throws IOException {
if (Files.exists(path) && Files.isRegularFile(path)) {
return Files.size(path);
} else {
logger.info("file doesn't exist or is not a file");
}
return 0;
}
/**
* 获取文件大小 : 用流的方式获取
*
* @param f
* @return
*/
public static long size4Stream(File f) {
FileChannel fc = null;
try {
if (f.exists() && f.isFile()) {
FileInputStream fis = new FileInputStream(f);
fc = fis.getChannel();
return fc.size();
} else {
logger.info("file doesn't exist or is not a file");
}
} catch (FileNotFoundException e) {
logger.severe("file doesn't exist or is not a file");
} catch (IOException e) {
logger.severe("The operation file is an IO exception.");
} finally {
if (null != fc) {
try {
fc.close();
} catch (IOException e) {
logger.severe("close FileInputStream IO exception.");
}
}
}
return 0;
}
/**
* 字符串csv格式 对象
*
@ -431,14 +302,15 @@ public class FileUtil {
*
* @param csvFilePath 文件地址
* @param cls 读取转化的对象
* @param hasHeader 是否存在header
* @param header 解析列对应的java字段用delimiter分割
* @param hasHeader csv文件中第一行是否是header
* @param delimiter 分隔符.默认,
* @param <I>
* @return
*/
public static <I> List<I> readCsv(final String csvFilePath, Class<I> cls, boolean hasHeader, char delimiter) {
public static <I> List<I> readCsv(final String csvFilePath, Class<I> cls, boolean hasHeader, String header, char delimiter) {
if (!isFileExist(csvFilePath)) {
throw new RuntimeException(String.format("解析用的csv\u0020[%s] 文件不存在。", csvFilePath));
throw new RuntimeException(String.format("解析用的csv [%s] 文件不存在。", csvFilePath));
}
if (StrUtil.isEmpty(delimiter)) {
delimiter = ',';
@ -446,7 +318,12 @@ public class FileUtil {
try {
File csvFile = new File(csvFilePath);
CsvMapper csvMapper = new CsvMapper();
CsvSchema csvSchema = csvMapper.typedSchemaFor(cls).withStrictHeaders(hasHeader).withColumnSeparator(delimiter).withComments();
CsvSchema.Builder builder = CsvSchema.builder();
if (StrUtil.isNotEmpty(header)) {
builder.addColumns(Arrays.asList(header.split(String.valueOf(delimiter))), CsvSchema.ColumnType.STRING);
}
CsvSchema csvSchema = builder.build().withColumnSeparator(delimiter).withSkipFirstDataRow(hasHeader).withStrictHeaders(hasHeader).withComments();
MappingIterator<I> recordIterator = csvMapper.readerWithTypedSchemaFor(cls).with(csvSchema).readValues(csvFile);
return recordIterator.readAll();
} catch (IOException e) {
@ -454,58 +331,45 @@ public class FileUtil {
}
}
/**
* 判断文件是否存在
*
* @param filePath
* @return
*/
public static boolean isFileExist(String filePath) {
if (StrUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
return file.exists() && !file.isDirectory();
}
/**
* 分段读取大文件
*
* @param path 文件路径
* @param csvFilePath 文件路径
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
* @param <T> 读取结果类型bean
* @return
*/
public <T> ReadFileBean<T> readBigFile(String path, ReadFileBean<T> readFileBean, Class<T> readCls) throws FileNotFoundException {
File file = new File(path);
if (!file.exists() || file.isDirectory()) {
throw new FileNotFoundException("file:" + path + " is not found.");
public static <T> ReadFileBean<T> readBigFile(String csvFilePath, ReadFileBean<T> readFileBean, Class<T> readCls) throws IOException {
if (!isFileExist(csvFilePath)) {
throw new FileNotFoundException(String.format("解析用的csv [%s] 文件不存在。", csvFilePath));
}
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
if (!csvFilePath.endsWith(TYPE_CSV)) {
throw new IOException(String.format("解析用的csv [%s] 文件不是CSV文件格式。", csvFilePath));
}
List<String> datas = new ArrayList<>();
try (RandomAccessFile randomAccessFile = new RandomAccessFile(new File(csvFilePath), "r")) {
if (readFileBean.getPointer() < 0) {
readFileBean.setPointer(0);
}
randomAccessFile.seek(readFileBean.getPointer());
readFileBean.setFileLength(randomAccessFile.length());
List<String> datas = new ArrayList<>();
int row = 1;
int row = 0;
String line;
while ((line = randomAccessFile.readLine()) != null && row <= readFileBean.getReadRowNum()) {
row++;
readFileBean.setPointer(randomAccessFile.getFilePointer());
datas.add(readFileBean.lineScavenge(charsetDecode(line, readFileBean.getReadCharset())));
}
}
if (StrUtil.isEmpty(datas)) {
//无数据
return readFileBean.setDatas(List.of());
return readFileBean.setDatas(new ArrayList<>());
}
if (path.contains(TYPE_CSV)) {
//csv文件处理
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
readFileBean.setHeader(csvToBean.getHeader());
if (csvToBean.isHasHeader()) {
if (csvToBean.hasHeader()) {
//文件存在header,设置header优先,没设置使用文件的
if (StrUtil.isNotEmpty(csvToBean.getHeader())) {
//替换header
@ -521,12 +385,6 @@ public class FileUtil {
List<T> dataList = readCsv(String.join("\n", datas), readCls, csvToBean.getDelimiter());
readFileBean.setDatas(dataList);
}
} catch (FileNotFoundException e) {
logger.severe("file exists." + e.getMessage());
} catch (IOException e) {
logger.severe("read file error." + e.getMessage());
}
return readFileBean;
}
@ -539,10 +397,8 @@ public class FileUtil {
*/
public static <T> com.yexuejc.base.pojo.CsvToBean getCsvToBean(Class<T> cls) {
CsvToBean annotation = cls.getAnnotation(CsvToBean.class);
Assert.notNull(annotation, cls.toString() + "类上需要添加注解@CsvToBean并指定header。");
com.yexuejc.base.pojo.CsvToBean csvToBean = new com.yexuejc.base.pojo.CsvToBean(
annotation.header(), annotation.delimiter(), annotation.hasHeader());
return csvToBean;
Assert.notNull(annotation, cls + "类上需要添加注解@CsvToBean并指定header。");
return new com.yexuejc.base.pojo.CsvToBean(annotation.header(), annotation.delimiter(), annotation.hasHeader());
}
/**

View File

@ -1,7 +1,7 @@
package com.yexuejc.base.util;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
@ -29,7 +29,7 @@ public final class StrUtil {
private StrUtil() {
}
private static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 判断字符串数组集合 是否为空(null""[],{})
@ -79,12 +79,12 @@ public final class StrUtil {
} else if (length < 1) {
return "";
} else {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length / 32; i++) {
sb.append(genUUID());
}
if (length % 32 > 0) {
sb.append(genUUID().substring(0, length % 32));
sb.append(genUUID(), 0, length % 32);
}
return sb.toString();
}
@ -100,14 +100,14 @@ public final class StrUtil {
*/
public static String genNum() {
int hashCode = UUID.randomUUID().toString().hashCode();
StringBuffer num = new StringBuffer();
StringBuilder num = new StringBuilder();
if (hashCode < 0) {
hashCode = 0 - hashCode;
hashCode = -hashCode;
num.append("0");
} else {
num.append("1");
}
return num.append(String.format("%010d", hashCode)).toString().substring(0, 8);
return num.append(String.format("%010d", hashCode)).substring(0, 8);
}
/**
@ -117,15 +117,15 @@ public final class StrUtil {
* @return 转换后字符串
*/
public static String toHex(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
StringBuilder sb = new StringBuilder(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
sb.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
sb.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
return sb.toString();
}
/**
@ -192,11 +192,7 @@ public final class StrUtil {
*/
public static String iso2utf(String str) {
String utfStr = null;
try {
utfStr = new String(str.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
utfStr = new String(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
return utfStr;
}
@ -206,14 +202,11 @@ public final class StrUtil {
* @param str
* @return
*/
private static Pattern pattern = Pattern.compile("[0-9]*");
private static final Pattern pattern = Pattern.compile("[0-9]*");
public static boolean isNumeric(String str) {
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
return isNum.matches();
}
/**
@ -232,7 +225,7 @@ public final class StrUtil {
for (int i = 0; i < 13; i++) {
coded.append(HEX_CHAR[random.nextInt(16)]);
}
coded.append(id.substring(0, 11));
coded.append(id, 0, 11);
for (int i = 0; i < 7; i++) {
coded.append(HEX_CHAR[random.nextInt(16)]);
}
@ -256,8 +249,8 @@ public final class StrUtil {
}
StringBuilder id = new StringBuilder();
id.append(coded.substring(13, 24));
id.append(coded.substring(31, 52));
id.append(coded, 13, 24);
id.append(coded, 31, 52);
return id.toString();
}
@ -277,7 +270,7 @@ public final class StrUtil {
return null;
}
Map<String, String> map = new HashMap<String, String>(16);
Map<String, String> map = new HashMap<>(16);
String[] kv = null;
for (String entry : entrys) {
if (isEmpty(entry)) {
@ -303,16 +296,15 @@ public final class StrUtil {
* @return
*/
public static String getSignContent(Map<String, ?> sortedParams) {
StringBuffer content = new StringBuffer();
StringBuilder content = new StringBuilder();
List<String> keys = new ArrayList<>(sortedParams.keySet());
Collections.sort(keys);
int index = 0;
for (int i = 0; i < keys.size(); ++i) {
String key = keys.get(i);
for (String key : keys) {
Object value = sortedParams.get(key);
if (isNotEmpty(key) && isNotEmpty(value)) {
content.append((index == 0 ? "" : "&") + key + "=" + value);
content.append(index == 0 ? "" : "&").append(key).append("=").append(value);
++index;
}
}
@ -343,8 +335,7 @@ public final class StrUtil {
List<String> keys = new ArrayList<>(sortedParams.keySet());
Collections.sort(keys);
int index = 0;
for (int i = 0; i < keys.size(); ++i) {
String key = keys.get(i);
for (String key : keys) {
Object value = sortedParams.get(key);
map.put(key, value);
++index;
@ -475,8 +466,7 @@ public final class StrUtil {
sb.append("Caused by: ").append(cClass).append(": ").append(eMessage).append(NEW_LINE);
for (StackTraceElement element : stackTrace) {
sb.append("\tat ");
sb.append(String.format(ERROR_MESSAGE_FORMAT, element.getClassName(), element.getMethodName(),
element.getFileName(), element.getLineNumber()));
sb.append(String.format(ERROR_MESSAGE_FORMAT, element.getClassName(), element.getMethodName(), element.getFileName(), element.getLineNumber()));
sb.append(NEW_LINE);
}
return sb.toString();

View File

@ -22,7 +22,7 @@ import java.util.logging.Logger;
* @date: 2017/12/28 16:12
*/
public class SysUtil {
private static Logger logger = Logger.getLogger(SysUtil.class.getName());
private static final Logger logger = Logger.getLogger(SysUtil.class.getName());
private static final String PROJECT_ROOT_PATH = "java.io.tmpdir";
private SysUtil() {
@ -43,8 +43,8 @@ public class SysUtil {
* @param clazz
* @return
*/
public static URL getRootPath(Class clazz, String filePath) {
return clazz.getClass().getResource(StrUtil.setStr(filePath, "/"));
public static URL getRootPath(Class<?> clazz, String filePath) {
return clazz.getResource(StrUtil.setStr(filePath, "/"));
}
/**
@ -73,9 +73,7 @@ public class SysUtil {
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
singleThreadPool.execute(() -> {
threadRun.execute();
});
singleThreadPool.execute(threadRun::execute);
singleThreadPool.shutdown();
}

View File

@ -0,0 +1,72 @@
package com.yexuejc.base.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import com.yexuejc.base.pojo.ReadFileBean;
import com.yexuejc.base.util.bean.AppnodeCertCsvBean;
/**
*
* @author: yexuejc
* @date: 2024/4/8 11:33
*/
public class FileUtilTest {
public static void main(String[] args) throws IOException {
readCsvFile();
// other();
}
private static void other() throws IOException {
System.out.println(FileUtil.getFileType("C:\\Users\\Administrator\\Desktop\\test.txt"));
boolean b = FileUtil.judeDirExists(new File("F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test\\a"));
File file = new File("F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test\\a\\test.txt");
FileUtil.judeFileExists(file);
System.out.println("创建文件夹:" + b);
System.out.println("SHA1:" + FileUtil.sha1(file));
//超大文件sha1
long l = System.currentTimeMillis();
System.out.println("SHA1:" + FileUtil.sha1(Paths.get("F:\\Docker\\win\\win10x64.iso").toFile()) + " 花费时间:" + (System.currentTimeMillis() - l));
System.out.println("MD5:" + FileUtil.md5(file));
//超大文件MD5
long l2 = System.currentTimeMillis();
System.out.println("MD5:" + FileUtil.md5(Paths.get("F:\\Docker\\win\\win10x64.iso").toFile()) + " 花费时间:" + (System.currentTimeMillis() - l2));
//超大文件MD5
long l3 = System.currentTimeMillis();
System.out.println("CRC32:" + FileUtil.crc32(Paths.get("F:\\Docker\\win\\win10x64.iso").toFile()) + " 花费时间:" + (System.currentTimeMillis() - l3));
String base64ToStr = FileUtil.base64ToStr(file);
System.out.println(base64ToStr);
String fileName = "F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test\\a\\test2.txt";
System.out.println(FileUtil.base64ToFile(base64ToStr, fileName));
File file2 = Paths.get(fileName).toFile();
System.out.println("SHA1:" + FileUtil.sha1(file2));
System.out.println("MD5:" + FileUtil.md5(file2));
System.out.println(FileUtil.size(file2.toPath()));
long l4 = System.currentTimeMillis();
System.out.println(FileUtil.size(Paths.get("F:\\Docker\\win\\win10x64.iso")) + " 花费时间:" + (System.currentTimeMillis() - l4));
}
private static void readCsvFile() throws IOException {
String path = "F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test.csv";
List<AppnodeCertCsvBean> list = FileUtil.readCsv(path, AppnodeCertCsvBean.class, true, "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", ',');
System.out.println("***********************************************");
System.out.println(JsonUtil.formatPrinter(list));
System.out.println("条数:" + list.size());
ReadFileBean<AppnodeCertCsvBean> readFileBean = new ReadFileBean<>(2);
do {
ReadFileBean<AppnodeCertCsvBean> bean = FileUtil.readBigFile(path, readFileBean, AppnodeCertCsvBean.class);
System.out.println("============================================");
System.out.println(JsonUtil.formatPrinter(bean));
} while (readFileBean.hasNext());
}
}

View File

@ -0,0 +1,22 @@
package com.yexuejc.base.util;
/**
*
* @author: yexuejc
* @date: 2024/4/8 11:22
*/
public class SysUtilTest {
public static void main(String[] args) {
System.out.println(SysUtil.getCachePath());
System.out.println(SysUtil.getRootPath(SysUtilTest.class, null));
SysUtil.threadRun("test", () -> {
String threadName = Thread.currentThread().getName();
System.out.println("当前线程的名称是:" + threadName);
});
SysUtil.getThreadList().forEach(t -> {
System.out.println("线程名称:" + t.getName());
});
SysUtil.checkJvmMemory();
System.out.println(SysUtil.jvmMemoryIsNotExecutable());
}
}

View File

@ -0,0 +1,105 @@
package com.yexuejc.base.util.bean;
import java.io.Serializable;
import com.yexuejc.base.annotation.CsvToBean;
/**
*
* @author: yexuejc
* @date: 2024/2/27 10:40
*/
@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", hasHeader = true)
public class AppnodeCertCsvBean implements Serializable {
/**是否生效Y/N*/
private String enable;
/**域名*/
private String domain;
/**
* 部署协议
* appnode
* ssh
* */
private String protocol;
/**
* 部署服务器
* 部署协议appnode: local 本机部署
* 部署协议appnode: 域名 部署的远程appnode域名
* 部署协议ssh : IP
* */
private String deployHost;
/**部署证书位置*/
private String deployPath;
/**服务器账号*/
private String uname;
/**服务器密码*/
private String pwd;
/**
* appnode协议时且远程部署时对应的远程appnode的ApiNodeId
*/
private Integer appnodeId;
public String getEnable() {
return enable;
}
public void setEnable(String enable) {
this.enable = enable;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getDeployHost() {
return deployHost;
}
public void setDeployHost(String deployHost) {
this.deployHost = deployHost;
}
public String getDeployPath() {
return deployPath;
}
public void setDeployPath(String deployPath) {
this.deployPath = deployPath;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getAppnodeId() {
return appnodeId;
}
public void setAppnodeId(Integer appnodeId) {
this.appnodeId = appnodeId;
}
}