mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-12-28 21:09:27 +08:00
Compare commits
21 Commits
1.5.2-jre8
...
1.5.2-jre1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59f0e6e296 | ||
|
|
8e8f390b5d | ||
|
|
2031a3aadf | ||
|
|
b6ffffef71 | ||
| 601611a11f | |||
| 8d364b99f2 | |||
| 127c6902bf | |||
| 007d8d2f16 | |||
| 41a9f1b507 | |||
| bf731e753a | |||
| 8d5e653d2b | |||
| c4fc4bd4f8 | |||
|
|
667ad26971 | ||
|
|
a260b41fae | ||
|
|
0754660c45 | ||
| 0dc13a6aa1 | |||
|
|
3ddd11f9c9 | ||
|
|
a05bb00123 | ||
|
|
77a0e29916 | ||
|
|
b0b8cbc7ab | ||
| 4c77c4ebe8 |
@@ -1,13 +1,13 @@
|
|||||||
yexuejc-base 基于jdk8常用工具包
|
yexuejc-base 基于jdk11常用工具包
|
||||||
----------------------
|
----------------------
|
||||||
源码地址:<br>
|
源码地址:<br>
|
||||||
github:https://github.com/yexuejc/yexuejc-base
|
github:https://github.com/yexuejc/yexuejc-base
|
||||||
gitee:https://gitee.com/jzsw-it/yexuejc-base
|
gitee:https://gitee.com/jzsw-it/yexuejc-base
|
||||||
|
|
||||||
### 说明
|
### 说明
|
||||||
1. 支持环境:java8(1.5.0开始支持java11,请使用`x.x.x-jre11`版本)
|
1. 支持环境:java11(1.5.0开始支持java11,请使用`1.5.x-jre11`版本)
|
||||||
2. 该工具包基于springboot提取,按理说适用于所有java工程
|
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>
|
<dependency>
|
||||||
<groupId>top.yexuejc</groupId>
|
<groupId>top.yexuejc</groupId>
|
||||||
<artifactId>yexuejc-base</artifactId>
|
<artifactId>yexuejc-base</artifactId>
|
||||||
<version>1.5.2-jre8</version>
|
<version>1.5.2-jre11</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
```
|
```
|
||||||
|
|
||||||
### 工具文档
|
### 工具文档
|
||||||
[Wiki](WIKI.md)
|
[Wiki](WIKI.md)
|
||||||
|
|
||||||
|
|||||||
12
UPDATE.md
12
UPDATE.md
@@ -1,17 +1,18 @@
|
|||||||
yexuejc-base 更新记录
|
yexuejc-base 更新记录
|
||||||
------------------
|
------------------
|
||||||
#### version :1.5.2-jre8
|
|
||||||
|
#### version :1.5.2-jre11
|
||||||
**time:2024-4-7 14:34:33** <br/>
|
**time:2024-4-7 14:34:33** <br/>
|
||||||
**branch:** jre8 <br/>
|
**branch:** jre11 <br/>
|
||||||
**update:** <br/>
|
**update:** <br/>
|
||||||
1. 升级相关依赖
|
1. 升级相关依赖
|
||||||
2. 依赖工具读取文件[FileInput.java](src/main/java/com/yexuejc/base/file/FileInput.java)从[FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)中提取出来
|
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)
|
3. 优化[FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)
|
||||||
4. 优化[JwtUtil.java](src/main/java/com/yexuejc/base/util/JwtUtil.java)
|
4. 优化[JwtUtil.java](src/main/java/com/yexuejc/base/util/JwtUtil.java)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
#### version :1.5.1-jre8
|
|
||||||
|
#### version :1.5.1-jre11
|
||||||
**time:2023-6-8 16:02:56** <br/>
|
**time:2023-6-8 16:02:56** <br/>
|
||||||
**branch:** jre11 <br/>
|
**branch:** jre11 <br/>
|
||||||
**update:** <br/>
|
**update:** <br/>
|
||||||
@@ -22,6 +23,9 @@ yexuejc-base 更新记录
|
|||||||
4. DateUtil,DateTimeUtil 时间操作工具优化
|
4. DateUtil,DateTimeUtil 时间操作工具优化
|
||||||
5. JsonUtil [feat] 增加json化时对LocalDateTime,LocalDate,Timestamp时间的优化,增加特殊场景序列化反序列化
|
5. JsonUtil [feat] 增加json化时对LocalDateTime,LocalDate,Timestamp时间的优化,增加特殊场景序列化反序列化
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
#### version :1.5.0-jre8
|
#### version :1.5.0-jre8
|
||||||
**time:2022-5-9 13:37:31** <br/>
|
**time:2022-5-9 13:37:31** <br/>
|
||||||
**branch:** master <br/>
|
**branch:** master <br/>
|
||||||
|
|||||||
10
pom.xml
10
pom.xml
@@ -6,11 +6,11 @@
|
|||||||
|
|
||||||
<groupId>top.yexuejc</groupId>
|
<groupId>top.yexuejc</groupId>
|
||||||
<artifactId>yexuejc-base</artifactId>
|
<artifactId>yexuejc-base</artifactId>
|
||||||
<version>1.5.2-jre8</version>
|
<version>1.5.2-jre11</version>
|
||||||
|
|
||||||
<name>yexuejc-base</name>
|
<name>yexuejc-base</name>
|
||||||
<url>https://github.com/yexuejc/yexuejc-base</url>
|
<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>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
|
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
|
||||||
<jjwt.version>0.12.5</jjwt.version>
|
<jjwt.version>0.12.5</jjwt.version>
|
||||||
<maven.compiler.verbose>true</maven.compiler.verbose>
|
<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>
|
<validation-api.version>3.0.2</validation-api.version>
|
||||||
|
|
||||||
<commons-io.version>2.11.0</commons-io.version>
|
<commons-io.version>2.11.0</commons-io.version>
|
||||||
@@ -155,8 +155,8 @@
|
|||||||
<version>3.13.0</version>
|
<version>3.13.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
<source>8</source>
|
<source>11</source>
|
||||||
<target>8</target>
|
<target>11</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!-- 打包源码 -->
|
<!-- 打包源码 -->
|
||||||
|
|||||||
173
src/main/java/com/yexuejc/base/encrypt/AES.java
Normal file
173
src/main/java/com/yexuejc/base/encrypt/AES.java
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
package com.yexuejc.base.encrypt;
|
||||||
|
|
||||||
|
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加解密
|
||||||
|
*
|
||||||
|
* @author maxf
|
||||||
|
* @class-name AES
|
||||||
|
* @description
|
||||||
|
* @date 2022/11/11 15:36
|
||||||
|
*/
|
||||||
|
public class AES {
|
||||||
|
public static AES builder() {
|
||||||
|
return Instace.aes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Instace {
|
||||||
|
private static AES aes = new AES();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String AES_ALGORITHM = "AES";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密模式
|
||||||
|
*/
|
||||||
|
public static enum ALGORITHM {
|
||||||
|
//算法/模式/填充 16字节加密后数据长度 不满16字节加密后长度
|
||||||
|
//AES/CBC/NoPadding 16 不支持
|
||||||
|
AES_CBC_NoPadding("AES/CBC/NoPadding"),
|
||||||
|
//AES/CBC/PKCS5Padding 32 16
|
||||||
|
AES_CBC_PKCS5Padding("AES/CBC/PKCS5Padding"),
|
||||||
|
//AES/CBC/ISO10126Padding 32 16
|
||||||
|
AES_CBC_ISO10126Padding("AES/CBC/ISO10126Padding"),
|
||||||
|
//AES/CFB/NoPadding 16 原始数据长度
|
||||||
|
AES_CFB_NoPadding("AES/CFB/NoPadding"),
|
||||||
|
//AES/CFB/PKCS5Padding 32 16
|
||||||
|
AES_CFB_PKCS5Padding("AES/CFB/PKCS5Padding"),
|
||||||
|
//AES/CFB/ISO10126Padding 32 16
|
||||||
|
AES_CFB_ISO10126Padding("AES/CFB/ISO10126Padding"),
|
||||||
|
//AES/ECB/NoPadding 16 不支持
|
||||||
|
AES_ECB_NoPadding("AES/ECB/NoPadding"),
|
||||||
|
//AES/ECB/PKCS5Padding 32 16
|
||||||
|
AES_ECB_PKCS5Padding("AES/ECB/PKCS5Padding"),
|
||||||
|
//AES/ECB/ISO10126Padding 32 16
|
||||||
|
AES_ECB_ISO10126Padding("AES/ECB/ISO10126Padding"),
|
||||||
|
//AES/OFB/NoPadding 16 原始数据长度
|
||||||
|
AES_OFB_NoPadding("AES/OFB/NoPadding"),
|
||||||
|
//AES/OFB/PKCS5Padding 32 16
|
||||||
|
AES_OFB_PKCS5Padding("AES/OFB/PKCS5Padding"),
|
||||||
|
//AES/OFB/ISO10126Padding 32 16
|
||||||
|
AES_OFB_ISO10126Padding("AES/OFB/ISO10126Padding"),
|
||||||
|
//AES/PCBC/NoPadding 16 不支持
|
||||||
|
AES_PCBC_NoPadding("AES/PCBC/NoPadding"),
|
||||||
|
//AES/PCBC/PKCS5Padding 32 16
|
||||||
|
AES_PCBC_PKCS5Padding("AES/PCBC/PKCS5Padding"),
|
||||||
|
//AES/PCBC/ISO10126Padding 32 16
|
||||||
|
AES_PCBC_ISO10126Padding("AES/PCBC/ISO10126Padding");
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
ALGORITHM(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ALGORITHM algorithm = ALGORITHM.AES_CBC_NoPadding;
|
||||||
|
private String key = "hj7x89H$yuBI0456";
|
||||||
|
private String iv = "NIfb&95GUY86Gfgh";
|
||||||
|
private Charset charset = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密
|
||||||
|
*
|
||||||
|
* @param data 明文
|
||||||
|
* @return 密文
|
||||||
|
* @Description AES算法加密明文
|
||||||
|
*/
|
||||||
|
public String encrypt(String data) throws Exception {
|
||||||
|
try {
|
||||||
|
|
||||||
|
Cipher cipher = Cipher.getInstance(algorithm.name);
|
||||||
|
int blockSize = cipher.getBlockSize();
|
||||||
|
byte[] dataBytes = data.getBytes(charset);
|
||||||
|
int plaintextLength = dataBytes.length;
|
||||||
|
if (plaintextLength % blockSize != 0) {
|
||||||
|
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
|
||||||
|
}
|
||||||
|
byte[] plaintext = new byte[plaintextLength];
|
||||||
|
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
|
||||||
|
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
||||||
|
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
|
||||||
|
byte[] encrypted = cipher.doFinal(plaintext);
|
||||||
|
return Base64.getEncoder().encodeToString(encrypted);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密
|
||||||
|
*
|
||||||
|
* @param data 密文
|
||||||
|
* @return 明文
|
||||||
|
* @Description AES算法解密密文
|
||||||
|
*/
|
||||||
|
public String decrypt(String data) throws Exception {
|
||||||
|
try {
|
||||||
|
byte[] encrypted = Base64.getDecoder().decode(data);
|
||||||
|
Cipher cipher = Cipher.getInstance(algorithm.name);
|
||||||
|
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
|
||||||
|
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();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// public static void main(String[] args) throws Exception {
|
||||||
|
// String str = " 奥萨蒂 asd8阿斯顿8asd ";
|
||||||
|
// AES.builder().setAlgorithm(ALGORITHM.AES_CBC_ISO10126Padding);
|
||||||
|
// AES.builder().setKey("DEsx89H$yuBI0456");
|
||||||
|
// String encrypt = AES.builder().encrypt(str);
|
||||||
|
// System.out.println(encrypt);
|
||||||
|
// String decrypt = AES.builder().decrypt(encrypt);
|
||||||
|
// System.out.println(">>>" + decrypt + "<<<");
|
||||||
|
// }
|
||||||
|
|
||||||
|
public ALGORITHM getAlgorithm() {
|
||||||
|
return algorithm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AES setAlgorithm(ALGORITHM algorithm) {
|
||||||
|
this.algorithm = algorithm;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AES setKey(String key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIv() {
|
||||||
|
return iv;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AES setIv(String iv) {
|
||||||
|
this.iv = iv;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Charset getCharset() {
|
||||||
|
return charset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AES setCharset(Charset charset) {
|
||||||
|
this.charset = charset;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
254
src/main/java/com/yexuejc/base/file/FileInput.java
Normal file
254
src/main/java/com/yexuejc/base/file/FileInput.java
Normal 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);
|
||||||
|
}*/
|
||||||
90
src/main/java/com/yexuejc/base/pojo/CreateZipFileBean.java
Normal file
90
src/main/java/com/yexuejc/base/pojo/CreateZipFileBean.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -134,7 +134,7 @@ public class JsonUtil {
|
|||||||
jsonMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
|
jsonMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
|
||||||
|
|
||||||
//值为空时,序列化所有值为“”
|
//值为空时,序列化所有值为“”
|
||||||
jsonMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
|
jsonMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<>() {
|
||||||
@Override
|
@Override
|
||||||
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||||
jsonGenerator.writeString("");
|
jsonGenerator.writeString("");
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public final class StrUtil {
|
|||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
return true;
|
return true;
|
||||||
} else if (obj instanceof Optional) {
|
} else if (obj instanceof Optional) {
|
||||||
return !((Optional<?>) obj).isPresent();
|
return ((Optional<?>) obj).isEmpty();
|
||||||
} else if (obj instanceof CharSequence) {
|
} else if (obj instanceof CharSequence) {
|
||||||
return ((CharSequence) obj).length() == 0;
|
return ((CharSequence) obj).length() == 0;
|
||||||
} else if (obj.getClass().isArray()) {
|
} else if (obj.getClass().isArray()) {
|
||||||
|
|||||||
65
src/main/java/com/yexuejc/base/util/ZipUtil.java
Normal file
65
src/main/java/com/yexuejc/base/util/ZipUtil.java
Normal 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)
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.yexuejc.base.util;
|
package com.yexuejc.base.util;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author: yexuejc
|
* @author: yexuejc
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import com.yexuejc.base.annotation.CsvToBean;
|
|||||||
* @author: yexuejc
|
* @author: yexuejc
|
||||||
* @date: 2024/2/27 10:40
|
* @date: 2024/2/27 10:40
|
||||||
*/
|
*/
|
||||||
|
@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", hasHeader = true)
|
||||||
public class AppnodeCertCsvBean implements Serializable {
|
public class AppnodeCertCsvBean implements Serializable {
|
||||||
/**是否生效:Y/N*/
|
/**是否生效:Y/N*/
|
||||||
private String enable;
|
private String enable;
|
||||||
|
|||||||
@@ -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,
|
|
||||||
|
Reference in New Issue
Block a user