21 Commits

Author SHA1 Message Date
its
59f0e6e296 [update] 1.5.2-jre11 发布 2024-04-08 16:26:04 +08:00
its
8e8f390b5d [update] JwtUtil 升级 2024-04-07 17:30:27 +08:00
its
2031a3aadf [update] 更新文档 2024-04-07 14:45:41 +08:00
its
b6ffffef71 [update] 1.5.2-jre11
1. 升级相关依赖
2. 依赖工具读取文件FileInput从FileUtil中提取出来
2024-04-07 14:37:36 +08:00
601611a11f update README.md.
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
2024-02-22 11:48:36 +00:00
8d364b99f2 [update] jsonUtil优化
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
2024-02-22 11:39:48 +00:00
127c6902bf [update] 判断空优化
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
2024-02-22 11:39:23 +00:00
007d8d2f16 [update] SysUtil 增加JVM内存判断等待锁
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
2023-12-26 09:40:38 +00:00
41a9f1b507 Merge remote-tracking branch 'origin/jre11' into jre11
# Conflicts:
#	src/main/java/com/yexuejc/base/util/JsonUtil.java
2023-09-20 10:02:40 +08:00
bf731e753a 增加StrUtil.printStackTrace方法:把异常堆栈信息转化为字符串 -> 替代 e.printStackTrace() 2023-09-20 10:01:58 +08:00
8d5e653d2b 增加StrUtil.printStackTrace方法:把异常堆栈信息转化为字符串 -> 替代 e.printStackTrace() 2023-09-20 10:01:27 +08:00
c4fc4bd4f8 [update]FileUtil优化分页读取 2023-07-19 20:14:15 +08:00
yexuejc007
667ad26971 [update] ObjUtil.copy方法优化(即使copy不成功,也不报错,出警告日志)。PR:copy源和目标对象中的属性类型不一致,copy不成功 2023-06-15 14:24:05 +08:00
yexuejc007
a260b41fae Merge remote-tracking branch 'origin/jre11' into jre11 2023-06-13 17:56:08 +08:00
yexuejc007
0754660c45 [feat] ObjUtil 增加对象的深度copy 2023-06-13 17:55:37 +08:00
0dc13a6aa1 1.5.1-jre11 2023-06-08 16:11:04 +08:00
yexuejc007
3ddd11f9c9 [update] JsonUtil 升级优化,增加特殊需求String的反序列化 2023-05-29 17:30:07 +08:00
yexuejc007
a05bb00123 [update] JsonUtil 升级优化,提供不同序列化反序列化的jsonmapper,处理反序列化时间值为空时的优化 2023-05-29 16:59:53 +08:00
yexuejc007
77a0e29916 [feat] 增加json化时对LocalDateTime,LocalDate,Timestamp时间的优化;
[version] 1.5.2-jdk11(未发布)
2023-05-17 14:02:28 +08:00
yexuejc007
b0b8cbc7ab [update] 日期时间工具丰富 2023-05-15 16:47:57 +08:00
4c77c4ebe8 AES加解密
文件压缩
FileUtil.base64ToStr 优化
兼容jdk11
2022-11-11 19:25:46 +08:00
18 changed files with 475 additions and 259 deletions

View File

@@ -1,13 +1,13 @@
yexuejc-base 基于jdk8常用工具包
yexuejc-base 基于jdk11常用工具包
----------------------
源码地址:<br>
github:https://github.com/yexuejc/yexuejc-base
gitee:https://gitee.com/jzsw-it/yexuejc-base
### 说明
1. 支持环境java81.5.0开始支持java11请使用`x.x.x-jre11`版本)
1. 支持环境java111.5.0开始支持java11请使用`1.5.x-jre11`版本)
2. 该工具包基于springboot提取按理说适用于所有java工程
3.`1.5.0`开始,版本分为`1.5.0-jre8``1.5.0-jre11`分别对于jre8和jre11使用
7.`1.5.0`开始,版本分为`1.5.0-jre8``1.5.0-jre11`分别对于jre8和jre11使用后续逐渐放弃jre8
### 使用
@@ -17,10 +17,11 @@ pom.xml
<dependency>
<groupId>top.yexuejc</groupId>
<artifactId>yexuejc-base</artifactId>
<version>1.5.2-jre8</version>
<version>1.5.2-jre11</version>
</dependency>
</dependencies>
```
### 工具文档
[Wiki](WIKI.md)

View File

@@ -1,35 +1,31 @@
yexuejc-base 更新记录
------------------
#### version 1.5.3-jre8
**time ** <br/>
**branch** jre8 <br/>
**update** <br/>
1. [FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)增加读取大文件自定义方法和单纯读取方法
2. 增加AES加解密: [com.yexuejc.base.encrypt.AES](src/main/java/com/yexuejc/base/encrypt/AES.java)
---
#### version 1.5.2-jre8
#### version 1.5.2-jre11
**time2024-4-7 14:34:33** <br/>
**branch** jre8 <br/>
**branch** jre11 <br/>
**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)
---
#### version 1.5.1-jre8
#### version 1.5.1-jre11
**time2023-6-8 16:02:56** <br/>
**branch** jre11 <br/>
**update** <br/>
1. FileUtil 增加读取csv文件分页读取
提供读取IO流方法合集
提供读取IO流方法合集
2. 增加AES加解密
3. 增加文件压缩ZipUtil
4. DateUtilDateTimeUtil 时间操作工具优化
5. JsonUtil [feat] 增加json化时对LocalDateTime,LocalDate,Timestamp时间的优化,增加特殊场景序列化反序列化
---
#### version 1.5.0-jre8
**time2022-5-9 13:37:31** <br/>
**branch** master <br/>

10
pom.xml
View File

@@ -6,11 +6,11 @@
<groupId>top.yexuejc</groupId>
<artifactId>yexuejc-base</artifactId>
<version>1.5.2-jre8</version>
<version>1.5.2-jre11</version>
<name>yexuejc-base</name>
<url>https://github.com/yexuejc/yexuejc-base</url>
<description>Common toolkits based on JDK8 packaging</description>
<description>Common toolkits based on JDK11+ packaging</description>
<licenses>
<license>
@@ -44,7 +44,7 @@
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
<jjwt.version>0.12.5</jjwt.version>
<maven.compiler.verbose>true</maven.compiler.verbose>
<java.version>1.8</java.version>
<java.version>11</java.version>
<validation-api.version>3.0.2</validation-api.version>
<commons-io.version>2.11.0</commons-io.version>
@@ -155,8 +155,8 @@
<version>3.13.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>8</source>
<target>8</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<!-- 打包源码 -->

View File

@@ -1,38 +0,0 @@
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.deser.std.StdScalarDeserializer;
/**
* 反序列化中内容为空时返回Integer为空
* <p>使用方式:@JsonDeserialize(using = IntegerNullValueDeserializer.class)</p>
* @author: yexuejc
* @date: 2024/4/15 18:08
*/
public class IntegerNullValueDeserializer extends StdScalarDeserializer<Integer> {
public IntegerNullValueDeserializer() {
super(Integer.class);
}
@Override
public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getValueAsString();
if (isInteger(value)) {
return super._parseInteger(p, ctxt, Integer.class);
} else {
return null;
}
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}

View File

@@ -17,7 +17,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException {
SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
}

View File

@@ -1,11 +1,11 @@
package com.yexuejc.base.encrypt;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* AES加解密
@@ -93,10 +93,7 @@ public class AES {
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
IvParameterSpec ivspec = null;
if (!algorithm.name.contains("ECB")) {
ivspec = new IvParameterSpec(iv.getBytes(charset));
}
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return Base64.getEncoder().encodeToString(encrypted);
@@ -118,10 +115,7 @@ public class AES {
byte[] encrypted = Base64.getDecoder().decode(data);
Cipher cipher = Cipher.getInstance(algorithm.name);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
IvParameterSpec ivspec = null;
if (!algorithm.name.contains("ECB")) {
ivspec = new IvParameterSpec(iv.getBytes(charset));
}
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted);
return new String(original, charset).trim();

View File

@@ -0,0 +1,254 @@
package com.yexuejc.base.file;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
/**
* 提供读取IO流方法合集
* <p>读取速度快慢顺序:</p>
* 1. {@link #read4Buffer(InputStream, Charset)} <br>
* 2. {@link #read4IOUtilsCopy(InputStream, Charset)} <br>
* 3. {@link #read4ByteStreams(InputStream, Charset)} <br>
* 4. {@link #read4Byte(InputStream, Charset)} <br>
* 5. {@link #read4StringBuilder(InputStream, Charset)} <br>
* 6. {@link #read4BufferedReaderParallel(InputStream, Charset, String)}<br>
* 7. {@link #read4BufferedReader(InputStream, Charset, String)}<br>
* 8. {@link #read4ScannerA(InputStream)}<br>
* 9. {@link #read4BufferIO(InputStream, Charset)}<br>
* 10. {@link #read4IOUtils(InputStream, Charset)}<br>
* 11. {@link #read4ScannerZ(InputStream)}<br>
* 12. {@link #read4CharStreams(InputStream, Charset)}<br>
*/
public class FileInput {
/**
* 读取IO流内容byte方式
*
* @param inputStream
* @param charset 编码:默认 {@link Charset#defaultCharset()}
* @return
* @throws IOException
*/
public static String read4Byte(InputStream inputStream, Charset charset) throws IOException {
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
return new String(bytes, charset == null ? Charset.defaultCharset() : charset);
}
/**
* 读取IO流内容BufferedReader方式
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @param lineSeparator 换行方式:默认跟随系统 {@link System#lineSeparator()}
* @return
*/
public static String read4BufferedReader(InputStream inputStream, Charset charset, String lineSeparator) {
return new BufferedReader(
new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset)
).lines().collect(Collectors.joining(lineSeparator == null ? System.lineSeparator() : lineSeparator));
}
/**
* 读取IO流内容BufferedReader 并行方式
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @param lineSeparator 换行方式:默认跟随系统 {@link System#lineSeparator()}
* @return
*/
public static String read4BufferedReaderParallel(InputStream inputStream, Charset charset, String lineSeparator) {
return new BufferedReader(
new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset)
).lines().parallel()
.collect(Collectors.joining(lineSeparator == null ? System.lineSeparator() : lineSeparator));
}
/**
* 读取IO流内容Scanner A方式
*
* @param inputStream
* @return
*/
public static String read4ScannerA(InputStream inputStream) {
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String str = s.hasNext() ? s.next() : "";
return str;
}
/**
* 读取IO流内容Scanner Z方式
*
* @param inputStream
* @return
*/
public static String read4ScannerZ(InputStream inputStream) {
return new Scanner(inputStream).useDelimiter("\\Z").next();
}
/**
* 读取IO流内容StringBuilder方式
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @return
*/
public static String read4StringBuilder(InputStream inputStream, Charset charset) throws IOException {
StringBuilder sb = new StringBuilder();
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
/**
* 读取IO流内容ByteArrayOutputStream方式
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @return
*/
public static String read4Buffer(InputStream inputStream, Charset charset) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(charset == null ? Charset.defaultCharset().name() : charset.name());
}
/**
* 读取IO流内容BufferedInputStream+ByteArrayOutputStream方式
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @return
*/
public static String read4BufferIO(InputStream inputStream, Charset charset) throws IOException {
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while (result != -1) {
buf.write((byte) result);
result = bis.read();
}
return buf.toString(charset == null ? Charset.defaultCharset().name() : charset.name());
}
/**
* 读取IO流内容 依赖于commons-io:commons-io {@link IOUtils#copy(Reader, OutputStream, Charset)}
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @return
* @throws IOException
*/
public static String read4IOUtilsCopy(InputStream inputStream, Charset charset) throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, charset == null ? Charset.defaultCharset() : charset);
return writer.toString();
}
/**
* 读取IO流内容 依赖于commons-io:commons-io {@link IOUtils#toString(InputStream, Charset)}
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @return
* @throws IOException
*/
public static String read4IOUtils(InputStream inputStream, Charset charset) throws IOException {
return IOUtils.toString(inputStream, charset == null ? Charset.defaultCharset() : charset);
}
/**
* 读取IO流内容 依赖于com.google.guava:guava {@link com.google.common.io.CharStreams#toString(Readable)}
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @return
* @throws IOException
*/
public static String read4CharStreams(InputStream inputStream, Charset charset) throws IOException, ClassNotFoundException {
try {
Class<?> charStreamsClass = Class.forName("com.google.common.io.CharStreams");
Method toStringMethod = charStreamsClass.getMethod("toString", InputStreamReader.class);
return (String) toStringMethod.invoke(null, new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("缺少依赖请引入Guava");
} catch (ReflectiveOperationException e) {
throw new RuntimeException("com.google.common.io.CharStreams.toString调用失败请检查Guava版本", e);
}
// return com.google.common.io.CharStreams.toString(new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
}
/**
* 读取IO流内容 依赖于com.google.guava:guava {@link com.google.common.io.ByteStreams#toByteArray(InputStream)}
*
* @param inputStream
* @param charset 编码:默认跟随系统 {@link Charset#defaultCharset()}
* @return
* @throws IOException
*/
public static String read4ByteStreams(InputStream inputStream, Charset charset) throws IOException, ClassNotFoundException {
try {
Class<?> charStreamsClass = Class.forName("com.google.common.io.ByteStreams");
Method toStringMethod = charStreamsClass.getMethod("toByteArray", InputStreamReader.class);
return (String) toStringMethod.invoke(null, new InputStreamReader(inputStream, charset == null ? Charset.defaultCharset() : charset));
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("缺少依赖请引入Guava");
} catch (ReflectiveOperationException e) {
throw new RuntimeException("com.google.common.io.ByteStreams.toByteArray调用失败请检查Guava版本", e);
}
// return new String(com.google.common.io.ByteStreams.toByteArray(inputStream), charset == null ? Charset.defaultCharset() : charset);
}
}
/*public static void main(String[] args) {
long size = FileUtil.size(new File("E:\\OS\\deepin-15.6-amd64\\DeepinCloudPrintServerInstaller_1.0.0.1.exe"));
System.out.println(size);
System.out.println(1024 * 1024 * 5);
if (size > 1024 * 1024 * 5) {
System.out.println("文件最大5M");
return;
}
long s1 = fileSize(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
System.out.println(s1);
long s2 = fileSize4Stream(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
System.out.println(s2);
String s1 = base64(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
System.out.println(s1);
String s = sha1(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
String s2 = sha1ByBigFile(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
System.out.println(s);
System.out.println(s2);
String md5 = md5(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
String md52 = md5ByBigFile(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
System.out.println(md5);
System.out.println(md52);
String crc32 = crc32(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
System.out.println(crc32);
}*/

View File

@@ -0,0 +1,90 @@
package com.yexuejc.base.pojo;
import java.util.List;
/**
* 压缩文件的参数设定
*
* @author maxf
* @class-name CreateZipFileBean
* @description 压缩文件参考 {@link com.yexuejc.base.util.ZipUtil}
* @date 2022/11/11 10:30
*/
public class CreateZipFileBean {
/**
* 压缩的源文件列表,为空表示压缩整个目录
*/
private List<String> sourceFileName;
/**
* 当压缩文件夹时,需要排除的文件。压缩文件时(该参数无效)
*/
private List<String> excludeFileName;
/**
* 压缩源文件的目录
*/
private String sourcePath;
/**
* 生成压缩文件路径(全路径+压缩文件名)
*/
private String zipFile;
/**
* 加密密码
*/
private String encryptPwd;
private String writeCharsetName = "UTF-8";
public List<String> getSourceFileName() {
return sourceFileName;
}
public String getEncryptPwd() {
return encryptPwd;
}
public CreateZipFileBean setEncryptPwd(String encryptPwd) {
this.encryptPwd = encryptPwd;
return this;
}
public CreateZipFileBean setSourceFileName(List<String> sourceFileName) {
this.sourceFileName = sourceFileName;
return this;
}
public String getSourcePath() {
return sourcePath;
}
public CreateZipFileBean setSourcePath(String sourcePath) {
this.sourcePath = sourcePath;
return this;
}
public String getZipFile() {
return zipFile;
}
public CreateZipFileBean setZipFile(String zipFile) {
this.zipFile = zipFile;
return this;
}
public String getWriteCharsetName() {
return writeCharsetName;
}
public CreateZipFileBean setWriteCharsetName(String writeCharsetName) {
this.writeCharsetName = writeCharsetName;
return this;
}
public List<String> getExcludeFileName() {
return excludeFileName;
}
public CreateZipFileBean setExcludeFileName(List<String> excludeFileName) {
this.excludeFileName = excludeFileName;
return this;
}
}

View File

@@ -18,7 +18,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.CRC32;
@@ -39,14 +38,11 @@ import io.jsonwebtoken.lang.Assert;
* @time 2017年11月3日 下午3:12:49
*/
public class FileUtil {
private static final Logger logger = Logger.getLogger(FileUtil.class.getName());
static Logger logger = Logger.getLogger(FileUtil.class.getName());
private FileUtil() {
}
private static final String NEW_LINE = "\n";
private static final String CONSTANT_DOT = ".";
private static final String TYPE_TAR_GZ = ".tar.gz";
private static final String TYPE_CSV = ".csv";
private static final String TAR_GZ = "tar.gz";
@@ -65,7 +61,7 @@ public class FileUtil {
if (fileName.lastIndexOf(TYPE_TAR_GZ) > 0) {
return TAR_GZ;
}
return fileName.substring(fileName.lastIndexOf(CONSTANT_DOT) + 1);
return fileName.substring(fileName.lastIndexOf(".") + 1);
} catch (Exception e) {
logger.severe("file doesn't exist or is not a file");
}
@@ -336,19 +332,22 @@ public class FileUtil {
}
/**
* 分段读取大文件(不限格式)
* 分段读取大文件
*
* @param filePath 文件路径
* @param csvFilePath 文件路径
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
* @param <T> 读取结果类型bean
* @return 文件分页读取内容(自定义处理后)及读取信息
* @return
*/
public static <T> ReadFileBean<T> readBigFile(String filePath, ReadFileBean<T> readFileBean, Function<List<String>, List<T>> readAfter) throws IOException {
if (!isFileExist(filePath)) {
throw new FileNotFoundException(String.format("[%s]文件不存在。", filePath));
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));
}
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(filePath), "r")) {
try (RandomAccessFile randomAccessFile = new RandomAccessFile(new File(csvFilePath), "r")) {
if (readFileBean.getPointer() < 0) {
readFileBean.setPointer(0);
}
@@ -366,59 +365,29 @@ public class FileUtil {
//无数据
return readFileBean.setDatas(new ArrayList<>());
}
List<T> dataList = readAfter.apply(datas);
//csv文件处理
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
readFileBean.setHeader(csvToBean.getHeader());
if (csvToBean.hasHeader()) {
//文件存在header,设置header优先,没设置使用文件的
if (StrUtil.isNotEmpty(csvToBean.getHeader())) {
//替换header
datas.remove(0);
datas.add(0, csvToBean.getHeader());
} else {
readFileBean.setHeader(datas.get(0));
}
} else {
//文件不存在header使用设置的
datas.add(0, csvToBean.getHeader());
}
List<T> dataList = readCsv(String.join("\n", datas), readCls, csvToBean.getDelimiter());
readFileBean.setDatas(dataList);
return readFileBean;
}
/**
* 分段读取大文件(不解析)
*
* @param csvFilePath 文件路径
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
* @return 文件分页读取内容每行为一个String对象及读取信息
*/
public static ReadFileBean<String> readBigFile(String csvFilePath, ReadFileBean<String> readFileBean) throws IOException {
return readBigFile(csvFilePath, readFileBean, (datas) -> datas);
}
/**
* 分段读取大文件(CSV格式)
*
* @param csvFilePath 文件路径
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
* @param <T> 读取结果类型bean
* @return 文件分页读取内容转bean后及读取信息
*/
public static <T> ReadFileBean<T> readBigFile(String csvFilePath, ReadFileBean<T> readFileBean, Class<T> readCls) throws IOException {
if (!csvFilePath.endsWith(TYPE_CSV)) {
throw new IOException(String.format("[%s]文件不是CSV文件格式。", csvFilePath));
}
return readBigFile(csvFilePath, readFileBean, (datas) -> {
//csv文件处理
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
readFileBean.setHeader(csvToBean.getHeader());
if (csvToBean.hasHeader()) {
//文件存在header,设置header优先,没设置使用文件的
if (StrUtil.isNotEmpty(csvToBean.getHeader())) {
//替换header
datas.remove(0);
datas.add(0, csvToBean.getHeader());
} else {
readFileBean.setHeader(datas.get(0));
}
} else {
//文件不存在header使用设置的
datas.add(0, csvToBean.getHeader());
}
try {
return readCsv(String.join(NEW_LINE, datas), readCls, csvToBean.getDelimiter());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
/**
* 获取csv的header,使用注解{@link CsvToBean}
*

View File

@@ -134,7 +134,7 @@ public class JsonUtil {
jsonMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
//值为空时,序列化所有值为“”
jsonMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
jsonMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString("");

View File

@@ -115,7 +115,7 @@ public class JwtUtil {
*/
public <T> T parse(String token, Class<T> cls) {
return JsonUtil.json2Obj(parseStr(token), cls);
}
}
/**
* 解密token为字符串
*
@@ -124,7 +124,7 @@ public class JwtUtil {
*/
public String parseStr(String token) {
return Jwts.parser().verifyWith(getSecretKey()).build().parseSignedClaims(token).getPayload().getSubject();
}
}
private SecretKey getSecretKey() {
byte[] bytes = Decoders.BASE64.decode(JWT_SIGNATURE_KEY);

View File

@@ -42,7 +42,7 @@ public final class StrUtil {
if (obj == null) {
return true;
} else if (obj instanceof Optional) {
return !((Optional<?>) obj).isPresent();
return ((Optional<?>) obj).isEmpty();
} else if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
} else if (obj.getClass().isArray()) {

View File

@@ -0,0 +1,65 @@
package com.yexuejc.base.util;
import com.yexuejc.base.pojo.CreateZipFileBean;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ExcludeFileFilter;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 压缩相关
* <p>依赖于zip4j<a href='https://github.com/srikanth-lingala/zip4j'>https://github.com/srikanth-lingala/zip4j</a></p>
*
* @author maxf
* @class-name ZipUtil
* @description 文件压缩
* @date 2022/11/11 11:09
*/
public class ZipUtil {
/**
* 创建压缩文件
*
* @param zipFileBean
*/
public static void createZipFile(CreateZipFileBean zipFileBean) throws ZipException {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setCompressionMethod(CompressionMethod.STORE);
//加密
zipParameters.setEncryptFiles(true);
zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
ZipFile zipFile = new ZipFile(zipFileBean.getZipFile());
if (StrUtil.isNotEmpty(zipFileBean.getEncryptPwd())) {
zipFile = new ZipFile(zipFileBean.getZipFile(), zipFileBean.getEncryptPwd().toCharArray());
}
if (StrUtil.isNotEmpty(zipFileBean.getSourceFileName())) {
//压缩文件
List<File> filesToAdd = new ArrayList<>(16);
Map<String, String> fileNamesMap = new HashMap<>(16);
zipFileBean.getSourceFileName().forEach(it -> {
String file = zipFileBean.getSourcePath() + File.separator + it;
filesToAdd.add(new File(file));
fileNamesMap.put(file, it);
});
zipFile.renameFiles(fileNamesMap);
zipFile.addFiles(filesToAdd, zipParameters);
} else {
//压缩目录
ExcludeFileFilter excludeFileFilter = zipFileBean.getExcludeFileName()::contains;
zipParameters.setExcludeFileFilter(excludeFileFilter);
zipFile.addFolder(new File(zipFileBean.getSourcePath()), zipParameters);
}
}
}
//checkemun key -> codeMst检索(缓存)
//返回exception(message用区分分割文言和exp)

View File

@@ -1,55 +0,0 @@
package com.yexuejc.base.encrypt;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class AESTest {
@Test
public void testEncrypt() throws Exception {
String data = "Hello World!";
AES aes = AES.builder()
.setAlgorithm(AES.ALGORITHM.AES_CBC_PKCS5Padding)
.setKey("hj7x89H$yuBI0456")
.setIv("NIfb&95GUY86Gfgh")
.setCharset(StandardCharsets.UTF_8);
String encrypted = aes.encrypt(data);
assertNotNull(encrypted);
assertFalse(encrypted.isEmpty());
}
@Test
public void testDecrypt() throws Exception {
String data = "SGVsbG8gV29ybGQh";
AES aes = AES.builder()
.setAlgorithm(AES.ALGORITHM.AES_CBC_PKCS5Padding)
.setKey("hj7x89H$yuBI0456")
.setIv("NIfb&95GUY86Gfgh")
.setCharset(StandardCharsets.UTF_8);
String decrypted = aes.decrypt(data);
assertNotNull(decrypted);
assertFalse(decrypted.isEmpty());
assertEquals("Hello World!", decrypted);
}
@Test
public void testEncryptAndDecrypt() throws Exception {
String data = "张三";
AES aes = AES.builder()
.setAlgorithm(AES.ALGORITHM.AES_OFB_ISO10126Padding)
.setKey("hj7x89H$yuBI0456")
.setIv("NIfb&95GUY86Gfgh")
.setCharset(StandardCharsets.UTF_8);
String encrypt = aes.encrypt(data);
System.out.println("加密:" + encrypt);
String decrypt = aes.decrypt(encrypt);
System.out.println("解密:" + decrypt);
}
}

View File

@@ -4,7 +4,6 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import com.yexuejc.base.pojo.ReadFileBean;
import com.yexuejc.base.util.bean.AppnodeCertCsvBean;
@@ -55,63 +54,19 @@ public class FileUtilTest {
}
private static void readCsvFile() throws IOException {
String path = "F:\\coding\\yexuejc-base\\src\\test\\java\\com\\yexuejc\\base\\util\\test.csv";
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());
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<String> readFileBean2 = new ReadFileBean<>(2);
ReadFileBean<String> bean2 = FileUtil.readBigFile(path, readFileBean2);
System.out.println("直接把每行读取成字符串============================================");
System.out.println(JsonUtil.formatPrinter(bean2));
System.out.println("直接把每行读取成字符串============================================");
//自定义每行数据的处理
ReadFileBean<AppnodeCertCsvBean> readFileBean1 = new ReadFileBean<>(2);
ReadFileBean<AppnodeCertCsvBean> bean1 = FileUtil.readBigFile(path, readFileBean1, datas -> {
if (readFileBean1.getStartRowNum() == 1) {
datas.remove(0);//跳过第一行
}
return datas.stream().map(str -> {
//自定义处理每一条数据
String[] split = str.split(",");
AppnodeCertCsvBean app = new AppnodeCertCsvBean();
app.setEnable(getValue(split, 0));
app.setDomain(getValue(split, 1));
app.setProtocol(getValue(split, 2));
app.setDeployHost(getValue(split, 3));
app.setDeployPath(getValue(split, 4));
app.setUname(getValue(split, 5));
app.setPwd(getValue(split, 6));
if (StrUtil.isNotEmpty(getValue(split, 7))) {
app.setAppnodeId(Integer.valueOf(getValue(split, 7)));
}
return app;
}).collect(Collectors.toList());
});
System.out.println("自定义每行数据的处理============================================");
System.out.println(JsonUtil.formatPrinter(bean1));
System.out.println("自定义每行数据的处理============================================");
//直接使用提供的csv文件读取
ReadFileBean<AppnodeCertCsvBean> readFileBean = new ReadFileBean<>(2);
do {
ReadFileBean<AppnodeCertCsvBean> bean = FileUtil.readBigFile(path, readFileBean, AppnodeCertCsvBean.class);
System.out.println("直接使用提供的csv文件读取============================================");
System.out.println("============================================");
System.out.println(JsonUtil.formatPrinter(bean));
System.out.println("直接使用提供的csv文件读取============================================");
} while (readFileBean.hasNext());
}
private static String getValue(String[] value, int index) {
try {
return value[index];
} catch (Exception e) {
return "";
}
}
}

View File

@@ -1,7 +1,5 @@
package com.yexuejc.base.util;
import java.util.Arrays;
/**
*
* @author: yexuejc

View File

@@ -2,16 +2,14 @@ package com.yexuejc.base.util.bean;
import java.io.Serializable;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.yexuejc.base.annotation.CsvToBean;
import com.yexuejc.base.converter.IntegerNullValueDeserializer;
/**
*
* @author: yexuejc
* @date: 2024/2/27 10:40
*/
@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId")
@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", hasHeader = true)
public class AppnodeCertCsvBean implements Serializable {
/**是否生效Y/N*/
private String enable;
@@ -39,7 +37,6 @@ public class AppnodeCertCsvBean implements Serializable {
/**
* appnode协议时且远程部署时对应的远程appnode的ApiNodeId
*/
@JsonDeserialize(using = IntegerNullValueDeserializer.class)
private Integer appnodeId;
public String getEnable() {

View File

@@ -1,10 +0,0 @@
是否生效,域名,部署协议,部署服务器,部署证书位置,服务器账号,服务器密码,appnodeId
N,kasm.mx.yexuejc.top,appnode,http://192.168.56.101:8888,,admin,admin,1
N,kasm.mx.yexuejc.top,appnode,local,,,,
N,shop.mx.yexuejc.top,appnode,local,,,,
Y,cloud.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
Y,blog.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
Y,yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
Y,jenkins.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
Y,git.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
Y,nexus.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
1 是否生效 域名 部署协议 部署服务器 部署证书位置 服务器账号 服务器密码 appnodeId
2 N kasm.mx.yexuejc.top appnode http://192.168.56.101:8888 admin admin 1
3 N kasm.mx.yexuejc.top appnode local
4 N shop.mx.yexuejc.top appnode local
5 Y cloud.yexuejc.top ssh 118.126.109.109:10371 /home/frpuser/http/cert/ frpuser yexuejc1
6 Y blog.yexuejc.top ssh 118.126.109.109:10371 /home/frpuser/http/cert/ frpuser yexuejc1
7 Y yexuejc.top ssh 118.126.109.109:10371 /home/frpuser/http/cert/ frpuser yexuejc1
8 Y jenkins.yexuejc.top ssh 118.126.109.109:10371 /home/frpuser/http/cert/ frpuser yexuejc1
9 Y git.yexuejc.top ssh 118.126.109.109:10371 /home/frpuser/http/cert/ frpuser yexuejc1
10 Y nexus.yexuejc.top ssh 118.126.109.109:10371 /home/frpuser/http/cert/ frpuser yexuejc1