18 Commits
1.0.0 ... 1.1.7

Author SHA1 Message Date
yexuejc
763bc7b47a 1.1.7 2018-08-17 11:23:28 +08:00
yexuejc
605d697c4f add .gitignore 2018-08-17 11:00:47 +08:00
c931ee8b5b 1.1.6 maven仓库变更 2018-07-07 11:34:29 +08:00
26c9b91cf0 1.1.6 maven仓库变更 2018-07-07 11:33:05 +08:00
f15f02e0d2 1.1.6 maven仓库变更 2018-07-07 11:13:37 +08:00
ee012fe845 1.1.5 2018-06-19 22:17:01 +08:00
25a534d307 1.1.3
修改正则RegexUtils.java
修改正则StrUtil.java->扩展genUUID()
DataTimeUtil.java ->扩展
2018-06-14 22:28:21 +08:00
0c12801047 1.1.3
修改正则RegexUtils.java
修改正则StrUtil.java->扩展genUUID()
DataTimeUtil.java ->扩展
2018-06-02 13:16:48 +08:00
b284ec4a84 1.1.3
修改正则RegexUtils.java
修改正则StrUtil.java->扩展genUUID()
2018-05-31 23:24:09 +08:00
ccf87e5a57 update readme 2018-05-16 15:07:03 +08:00
32675d5d29 1.1.2 修改依赖 2018-05-16 15:04:18 +08:00
c7e91286dc 1.1.1 2018-05-15 15:19:47 +08:00
1beed4e029 first commit 2018-05-15 14:25:04 +08:00
0417621cfe 1.1.0 2018-05-14 20:51:02 +08:00
63e3b39c62 1.1.0 2018-05-13 11:03:52 +08:00
43e6ea9045 1.0.1 2018-05-12 22:42:12 +08:00
4b7e86de85 1.0.1 2018-05-12 22:36:03 +08:00
051ddc1eb4 first commit 2018-04-09 15:19:17 +08:00
19 changed files with 1029 additions and 142 deletions

25
.gitignore vendored Normal file
View File

@@ -0,0 +1,25 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
.mvn
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: javax.validation:validation-api:1.1.0.Final">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final-sources.jar!/" />
</SOURCES>
</library>
</component>

33
README.md Normal file
View File

@@ -0,0 +1,33 @@
通用工具包
### 说明
>1. 支持环境java8
>2. 该工具包基于springboot提取按理说适用于所有java工程
>3. 其中依赖jjwt、validation-api但不传递依赖
### 使用
>yexuejc.base.version=1.1.6
pom.xml
```
<dependencies>
<dependency>
<groupId>com.github.yexuejc</groupId>
<artifactId>yexuejc-base</artifactId>
<version>${yexuejc.base.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```
### 工具文档
[Wiki](WIKI.md)
### 更新日志
[更新记录](UPDATE.md)

66
UPDATE.md Normal file
View File

@@ -0,0 +1,66 @@
yexuejc-base 更新记录
------------------
#### version 1.1.7
**time2018-8-17 11:22:50** <br/>
**branch** master <br/>
**update** <br/>
>1. 优化ApiVO
#
#### version 1.1.6
**time2018-7-7 11:32:56** <br/>
**branch** master <br/>
**update** <br/>
>1. maven仓库更新
#
#### version 1.1.5
**time2018-6-19 22:16:34** <br/>
**branch** master <br/>
**update** <br/>
>1. 优化ApiVO
#
#### version 1.1.4
**time2018-6-14 22:27:59** <br/>
**branch** master <br/>
**update** <br/>
>1. 统一编码UTF-8
#
#### version 1.1.3
**time2018年6月2日12:16:58** <br/>
**branch** master <br/>
**update** <br/>
>1. 修改正则RegexUtils.java
>2. 修改正则StrUtil.java->扩展genUUID()
#
#### version 1.1.2
**time** 2018-5-16 15:03:28<br/>
**branch** master <br/>
**update** <br/>
>1. 修改依赖
#
#### version 1.1.1
**time** 2018-5-12 22:25:05<br/>
**branch** master <br/>
**update** <br/>
>1. 添加RSA
#
##### version 1.1.0
**time** 2018-5-12 22:25:05<br/>
**branch** master <br/>
**update** <br/>
>1. 添加支持加密功能
#
#### version 1.0.0
**time** 2018-1-31 12:16:10<br/>
**branch** master <br/>
**update** <br/>
>1. 基于java8开发的web应用工具包
#

9
WIKI.md Normal file
View File

@@ -0,0 +1,9 @@
yexuejc-base 文档
------------------
##### 1. RespsConsts 网络请求统一返回 常量
##### 2. Resps 网络请求统一返回类
##### 3. ApiVO 接口交互API
##### 4. BaseVO 基类VO
待完善......

46
pom.xml
View File

@@ -6,20 +6,20 @@
<groupId>com.yexuejc.base</groupId>
<artifactId>yexuejc-base</artifactId>
<version>1.0.0</version>
<version>1.1.7</version>
<name>${project.artifactId}</name>
<properties>
<repos.yexuejc.ivt.url>http://47.100.13.178:7081/nexus/content/groups/public</repos.yexuejc.ivt.url>
<repos.yexuejc.prod.url>http://47.100.13.178:7081/nexus/content/groups/public</repos.yexuejc.prod.url>
<repos.yexuejc.dist.url>http://47.100.13.178:7081/nexus/content/repositories</repos.yexuejc.dist.url>
<repos.yexuejc.url>https://nexus.yexuejc.club/repository/</repos.yexuejc.url>
<repos.aliyun.url>http://maven.aliyun.com/nexus/content/groups/public</repos.aliyun.url>
<repos.jitpack.url>https://jitpack.io</repos.jitpack.url>
<jjwt.version>0.7.0</jjwt.version>
<maven.compiler.verbose>true</maven.compiler.verbose>
<java.version>1.8</java.version>
<validation-api.version>1.1.0.Final</validation-api.version>
<commons-codec.version>1.10</commons-codec.version>
<commons-io.version>2.6</commons-io.version>
</properties>
<dependencies>
@@ -28,7 +28,25 @@
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jjwt.version}</version>
<optional>true</optional>
</dependency>
<!-- 数据校验框架 -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation-api.version}</version>
</dependency>
<!--base64使用到的依赖-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
<scope>compile</scope>
</dependency>
<!--IOUtils使用到的依赖-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
@@ -40,6 +58,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
@@ -78,14 +97,9 @@
</build>
<repositories>
<repository>
<id>yexuejc-prod-nexus-public</id>
<name>yexuejc-prod-nexus-public</name>
<url>${repos.yexuejc.prod.url}</url>
</repository>
<repository>
<id>yexuejc-ivt-nexus-public</id>
<name>yexuejc-ivt-nexus-public</name>
<url>${repos.yexuejc.ivt.url}</url>
<id>yexuejc-nexus-public</id>
<name>yexuejc-nexus-public</name>
<url>${repos.yexuejc.url}maven-public/</url>
</repository>
<repository>
<id>aliyun-nexus-public</id>
@@ -102,12 +116,12 @@
<repository>
<id>releases</id>
<name>nexus-release</name>
<url>${repos.yexuejc.dist.url}/releases</url>
<url>${repos.yexuejc.url}maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>nexus-snapshots</name>
<url>${repos.yexuejc.dist.url}/snapshots</url>
<url>${repos.yexuejc.url}maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>

View File

@@ -0,0 +1,187 @@
package com.yexuejc.base.encrypt;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
/**
* RSA加解密 配置模式
*
* @ClassName: RSA
* @Description:
* @author: maxf
* @date: 2018/5/15 14:39
*/
public class RSA {
public static final String CHARSET = "UTF-8";
public static final String RSA_ALGORITHM = "RSA";
public static Map<String, String> initKeys(int keySize) {
//为RSA算法创建一个KeyPairGenerator对象
KeyPairGenerator kpg;
try {
kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("No such algorithm-->[" + RSA_ALGORITHM + "]");
}
//初始化KeyPairGenerator对象,密钥长度
kpg.initialize(keySize);
//生成密匙对
KeyPair keyPair = kpg.generateKeyPair();
//得到公钥
Key publicKey = keyPair.getPublic();
String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
//得到私钥
Key privateKey = keyPair.getPrivate();
String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
Map<String, String> keyPairMap = new HashMap<String, String>();
keyPairMap.put("publicKey", publicKeyStr);
keyPairMap.put("privateKey", privateKeyStr);
return keyPairMap;
}
/**
* 得到公钥
*
* @param publicKey 密钥字符串经过base64编码
* @throws Exception
*/
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
//通过X509编码的Key指令获得公钥对象
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
return key;
}
/**
* 得到私钥
*
* @param privateKey 密钥字符串经过base64编码
* @throws Exception
*/
public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
//通过PKCS#8编码的Key指令获得私钥对象
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
return key;
}
/**
* 公钥加密
*
* @param data
* @param publicKey
* @return
*/
public static String publicEncrypt(String data, RSAPublicKey publicKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
} catch (Exception e) {
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
}
}
/**
* 私钥解密
*
* @param data
* @param privateKey
* @return
*/
public static String privateDecrypt(String data, RSAPrivateKey privateKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), privateKey.getModulus().bitLength()), CHARSET);
} catch (Exception e) {
throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
}
}
/**
* 私钥加密
*
* @param data
* @param privateKey
* @return
*/
public static String privateEncrypt(String data, RSAPrivateKey privateKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength()));
} catch (Exception e) {
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
}
}
/**
* 公钥解密
*
* @param data
* @param publicKey
* @return
*/
public static String publicDecrypt(String data, RSAPublicKey publicKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), publicKey.getModulus().bitLength()), CHARSET);
} catch (Exception e) {
throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
}
}
private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas, int keySize) {
int maxBlock = 0;
if (opmode == Cipher.DECRYPT_MODE) {
maxBlock = keySize / 8;
} else {
maxBlock = keySize / 8 - 11;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] buff;
int i = 0;
try {
while (datas.length > offSet) {
if (datas.length - offSet > maxBlock) {
buff = cipher.doFinal(datas, offSet, maxBlock);
} else {
buff = cipher.doFinal(datas, offSet, datas.length - offSet);
}
out.write(buff, 0, buff.length);
i++;
offSet = i * maxBlock;
}
} catch (Exception e) {
throw new RuntimeException("加解密阀值为[" + maxBlock + "]的数据时发生异常", e);
}
byte[] resultDatas = out.toByteArray();
IOUtils.closeQuietly(out);
return resultDatas;
}
}

View File

@@ -0,0 +1,62 @@
package com.yexuejc.base.encrypt;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
/**
* RSA加解密 证书模式
* 依赖 {@link RSA}
* @ClassName: RSA2
* @Description:
* @author: maxf
* @date: 2018/5/15 14:37
*/
public class RSA2 {
public static final String CHARSET = "UTF-8";
public static final String RSA_ALGORITHM = "RSA";
/**
* 得到公钥
*
* @param filepath 密钥文件路径
* @throws Exception
*/
public static RSAPublicKey getPublicKey(String filepath) throws CertificateException, FileNotFoundException {
//通过证书,获取公钥
CertificateFactory cf = null;
cf = CertificateFactory.getInstance("X.509");
Certificate c = cf.generateCertificate(new FileInputStream(filepath));
return (RSAPublicKey) c.getPublicKey();
}
/**
* 得到私钥
*
* @param filepath 私钥路径
* @param alias 证书别名
* @param password 证书密码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws IOException
* @throws CertificateException
* @throws UnrecoverableKeyException
*/
public static RSAPrivateKey getPrivateKey(String filepath, String alias, String password) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(filepath), password.toCharArray());
return (RSAPrivateKey) ks.getKey(alias, password.toCharArray());
}
}

View File

@@ -20,6 +20,10 @@ public class Resps<T> implements Serializable {
* 状态
*/
private String code;
/**
* md5码
*/
private String sign;
/**
* 内容
*/
@@ -77,6 +81,10 @@ public class Resps<T> implements Serializable {
return this;
}
public static Resps success(String[] msg) {
return new Resps(RespsConsts.CODE_SUCCESS, msg);
}
public static Resps success(String msg) {
return new Resps(RespsConsts.CODE_SUCCESS, msg);
}
@@ -89,8 +97,8 @@ public class Resps<T> implements Serializable {
return new Resps(RespsConsts.CODE_ERROR, msg);
}
public static Resps fail(String msg) {
return new Resps(RespsConsts.CODE_FAIL, msg);
public static Resps error(String[] msg) {
return new Resps(RespsConsts.CODE_ERROR, msg);
}
public static Resps error(String code, String msg) {
@@ -101,6 +109,14 @@ public class Resps<T> implements Serializable {
return new Resps(code, msg);
}
public static Resps fail(String msg) {
return new Resps(RespsConsts.CODE_FAIL, msg);
}
public static Resps fail(String[] msg) {
return new Resps(RespsConsts.CODE_FAIL, msg);
}
public static Resps fail(String code, String msg) {
return new Resps(code, msg);
}
@@ -133,6 +149,14 @@ public class Resps<T> implements Serializable {
this.msg = msg;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
@Override
public String toString() {
return JsonUtil.obj2Json(this);

View File

@@ -18,12 +18,58 @@ public class ApiVO implements Serializable {
public ApiVO() {
}
public ApiVO(STATUS status) {
this.status = status;
}
/**
* 使用默认返回code
*
* @param status
* @param msg
*/
public ApiVO(STATUS status, String msg) {
this.status = status;
this.msgs = StrUtil.isNotEmpty(msg) ? new String[]{msg} : null;
}
/**
* 使用默认返回code
*
* @param status
* @param msg
*/
public ApiVO(STATUS status, String[] msg) {
this.status = status;
this.msgs = msg;
}
public ApiVO(STATUS status, String code, String msg) {
this.status = status;
this.code = code;
this.msgs = StrUtil.isNotEmpty(msg) ? new String[]{msg} : null;
}
public ApiVO(STATUS status, String code, String[] msg) {
this.status = status;
this.code = code;
this.msgs = msg;
}
public ApiVO setStatus(STATUS status, String code, String msg) {
this.status = status;
this.code = code;
this.msgs = StrUtil.isNotEmpty(msg) ? new String[]{msg} : null;
return this;
}
public ApiVO setStatus(STATUS status, String code, String[] msg) {
this.status = status;
this.code = code;
this.msgs = msg;
return this;
}
public enum STATUS {
/**
* 成功
@@ -62,36 +108,24 @@ public class ApiVO implements Serializable {
*/
private Object object2;
public <T extends Object> void setObject1(T obj) {
public <T extends Object> ApiVO setObject1(T obj) {
object1 = obj;
return this;
}
public <T extends Object> T getObject1(Class<T> clazz) {
return (T) object1;
}
public <T extends Object> void setObject2(T obj) {
public <T extends Object> ApiVO setObject2(T obj) {
object2 = obj;
return this;
}
public <T extends Object> T getObject2(Class<T> clazz) {
return (T) object2;
}
public ApiVO setStatus(STATUS status, String code, String msg) {
this.status = status;
this.code = code;
this.msgs = StrUtil.isNotEmpty(msg) ? new String[]{msg} : null;
return this;
}
public ApiVO setStatus(STATUS status, String code, String[] msg) {
this.status = status;
this.code = code;
this.msgs = msg;
return this;
}
public boolean isSucc() {
if (STATUS.S.name().equals(status.name())) {
return true;
@@ -106,8 +140,9 @@ public class ApiVO implements Serializable {
return false;
}
public void setMsg(String msg) {
public ApiVO setMsg(String msg) {
this.msgs = StrUtil.isNotEmpty(msg) ? new String[]{msg} : null;
return this;
}
public boolean isFail() {
@@ -118,8 +153,9 @@ public class ApiVO implements Serializable {
return code;
}
public void setCode(String code) {
public ApiVO setCode(String code) {
this.code = code;
return this;
}
@Override
@@ -131,12 +167,14 @@ public class ApiVO implements Serializable {
return status;
}
public void setStatus(STATUS status) {
public ApiVO setStatus(STATUS status) {
this.status = status;
return this;
}
public void setMsgs(String[] msgs) {
public ApiVO setMsgs(String[] msgs) {
this.msgs = msgs;
return this;
}
public String[] getMsgs() {

View File

@@ -5,6 +5,8 @@ import com.yexuejc.base.util.JsonUtil;
import java.io.Serializable;
/**
* 基类VO
*
* @PackageName: com.yexuejc.util.base.pojo
* @Description:
* @author: maxf
@@ -14,16 +16,16 @@ public class BaseVO implements Serializable {
private static final long serialVersionUID = -1442656950873492155L;
public static interface Add {
public interface Add {
}
public static interface Del {
public interface Del {
}
public static interface Mdfy {
public interface Mdfy {
}
public static interface Srch {
public interface Srch {
}
@Override

View File

@@ -0,0 +1,49 @@
package com.yexuejc.base.pojo;
import com.yexuejc.base.util.JsonUtil;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
* 分页 VO
*
* @author: maxf
* @date: 2018/3/28 14:23
*/
public class PagerVO extends BaseVO {
private static final long serialVersionUID = 3490440129554644587L;
@NotNull
@Min(1L)
private Integer page = 1;
@NotNull
@Min(1L)
private Integer size = 20;
public int getOffset() {
return (this.page - 1) * this.size;
}
@Override
public String toString() {
return JsonUtil.obj2Json(this);
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}

View File

@@ -0,0 +1,45 @@
package com.yexuejc.base.pojo;
import com.yexuejc.base.util.JsonUtil;
import java.io.Serializable;
/**
* 解密前的请求参数格式
*
* @author: maxf
* @date: 2018/5/12 14:52
*/
public class ParamsPO implements Serializable {
private static final long serialVersionUID = 9171765814642105098L;
/**
* 参数
*/
private String data;
/**
* md5
*/
private String sign;
@Override
public String toString() {
return JsonUtil.obj2Json(this);
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
}

View File

@@ -136,7 +136,7 @@ public class DateTimeUtil {
* @param zonedDateTime
* @return
*/
public static Date zonedDateTime2Date(ZonedDateTime zonedDateTime) {
public static Date parseDate(ZonedDateTime zonedDateTime) {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = zonedDateTime.withZoneSameInstant(zoneId);
Date date = Date.from(zdt.toInstant());
@@ -149,12 +149,99 @@ public class DateTimeUtil {
* @param date
* @return
*/
public static ZonedDateTime date2ZonedDateTime(Date date) {
public static ZonedDateTime parseZonedDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).withZoneSameInstant(zoneId);
}
/**
* Date 转 LocalDateTime
*
* @param date
* @return
*/
public static LocalDateTime parseLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalDateTime();
}
/**
* LocalDateTime 转 Date
*
* @param localDateTime
* @return
*/
public static Date parseLocalDateTime(LocalDateTime localDateTime) {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDateTime.atZone(zoneId);
return Date.from(zdt.toInstant());
}
/**
* LocalDate 转 Date
*
* @param localDate
* @return
*/
public static Date parseData(LocalDate localDate) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
return Date.from(instant);
}
/**
* Date 转 LocalDate
*
* @param date
* @return
*/
public static LocalDate parseLocalDate(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime.toLocalDate();
}
/**
* Date 转 LocalTime
*
* @param date
* @return
*/
public static LocalTime parseLocalTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime.toLocalTime();
}
/**
* Date 转 LocalTime
*
* @param localTime
* @return 当前日期的指定时间
*/
public static Date parseDate(LocalTime localTime) {
LocalDate localDate = LocalDate.now();
return parseDate(localDate, localTime);
}
/**
* Date 转 LocalTime
*
* @param localDate
* @param localTime
* @return 指定日期的指定时间
*/
public static Date parseDate(LocalDate localDate, LocalTime localTime) {
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}
/**
* 格式化时间 <br/>
* 格式 yyyy-MM-dd HH:mm:ss

View File

@@ -2,6 +2,7 @@ package com.yexuejc.base.util;
/**
* excel 格式验证工具
*
* @ClassName: ExcelImportUtils
* @Description:
* @author: maxf

View File

@@ -12,7 +12,7 @@ public class JwtUtil {
/** token类型 */
private static String JWT_HEADER_TYP = "JWT";
/** token发行商 */
private static String JWT_CLAIMS_ISS = "uselaw.com";
private static String JWT_CLAIMS_ISS = "yexuejc.com";
/**
* 加密内容生成token

View File

@@ -32,6 +32,56 @@ public class RegexUtils {
* cvn23位数字
*/
public static final String REGEX_NUM3 = "^$|^\\d{3}$";
/**
* ID用 正则表达式(32位 16进制小写编码)
*/
public static final String REGEX_ID = "^$|^[a-f0-9]{32}$";
/**
* 手机用 正则表达式(首位为1,共11位数字)
*/
public static final String REGEX_MOBILE = "^$|^1\\d{10}$";
/**
* 验证是否是Json数据 正则表达式(首尾是{})
*/
public static final String REGEX_JSON = "^$|^\\{.*\\}$";
/**
* 验证预约日期时间 正则表达式(精确到半小时)
*/
public static final String REGEX_DATE_APPOINTMENT = "^$|^\\d{4}-[01]\\d-[0-3]\\d [0-2]\\d:(0|3)0$";
/**
* 日期 正则表达式
*/
public static final String REGEX_DATE = "^$|^\\d{4}-[01]\\d-[0-3]\\d$";
/**
* 6位数字
*/
public static final String REGEX_NUM6 = "^$|^\\d{6}$";
/**
* 两位以内正整数
*/
public static final String REGEX_PINT2 = "^$|^[1-9]\\d{0,1}$";
/**
* 五位以内正整数
*/
public static final String REGEX_PINT5 = "^$|^[1-9]\\d{0,4}$";
/**
* 十位以内正整数
*/
public static final String REGEX_PINT10 = "^$|^[1-9]\\d{0,9}$";
/**
* 十位以内正整或0
*/
public static final String REGEX_INT10 = "^$|^[1-9]\\d{0,9}$|^0$";
/**
* 可有8位整数,2位小数
*/
public static final String REGEX_PFLOAT10_2 = "^$|^(\\d\\.\\d{1})|([1-9]\\d{0,7}(\\.\\d{1,2})?)$";
/**
* STS RoleSessionName
*/
public static final String REGEX_STS_ROLE_SESSION_NAME = "^[a-zA-Z0-9\\.@\\-_]+$";
/**
* 正则:入参验证

View File

@@ -3,37 +3,52 @@ package com.yexuejc.base.util;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.compile;
/**
* @PackageName: com.yexuejc.util.base.util
* 字符串工具类
*
* @ClassName: StrUtil
* @Description:
* @author: maxf
* @date: 2017/12/28 17:34
* @date: 2018/5/12 19:13
*/
public final class StrUtil {
public static char[] HEX_CHAR = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f'};
private StrUtil() {
}
public static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 判断字符串,数组,集合 是否为空
*
* @param obj
* @return
*/
public static boolean isEmpty(Object obj) {
if (obj instanceof String) {
return obj == null || "".equals((String) obj);
if (obj == null || "".equals((String) obj)) {
return true;
} else {
return false;
}
} else if (obj instanceof Object[]) {
return obj == null || ((Object[]) ((Object[]) obj)).length == 0;
} else if (obj instanceof Collection) {
return obj == null || ((Collection) obj).size() == 0;
if (obj == null || ((Object[]) obj).length == 0) {
return true;
} else {
return false;
}
} else if (obj instanceof Collection<?>) {
if (obj == null || ((Collection<?>) obj).size() == 0) {
return true;
} else {
return false;
}
} else {
return obj == null;
if (obj == null) {
return true;
} else {
return false;
}
}
}
@@ -41,10 +56,46 @@ public final class StrUtil {
return !isEmpty(obj);
}
/**
* 生成32位UUID
*
* @return
*/
public static String genUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
/**
* 生成指定位数UUID
*
* @param length
* @return
*/
public static String genUUID(int length) {
if (length <= 32) {
return genUUID().substring(0, length);
} else if (length < 1) {
return "";
} else {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length / 32; i++) {
sb.append(genUUID());
}
if (length % 32 > 0) {
sb.append(genUUID().substring(0, length % 32));
}
return sb.toString();
}
}
/**
* 生成11位编号可以用作订单号有很小几率出现重复需要做异常处理<br/>
* 左边第一位为正负标识正数1 负数0<br/>
* 剩余位数为UUID的hashcode值<br/>
* 可以再生成的编号基础上嵌入其他标识编码
*
* @return
*/
public static String genNum() {
int hashCode = UUID.randomUUID().toString().hashCode();
StringBuffer num = new StringBuffer();
@@ -54,131 +105,215 @@ public final class StrUtil {
} else {
num.append("1");
}
return num.append(String.format("%010d", hashCode)).toString().substring(0, 8);
}
/**
* 解析aa=bb&cc=dd&ee=ff格式的字符串返回HashMap
*
* @param urlencoded
* @return
*/
public static Map<String, String> parseUrlencoded(String urlencoded) {
if (isEmpty(urlencoded)) {
return null;
} else {
String[] entrys = urlencoded.split("&");
if (isEmpty(entrys)) {
return null;
}
String[] entrys = urlencoded.split("&");
if (isEmpty(entrys)) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
String[] kv = null;
for (String entry : entrys) {
if (isEmpty(entry)) {
continue;
}
kv = entry.split("=");
if (isEmpty(kv)) {
continue;
}
if (kv.length > 1) {
map.put(kv[0], kv[1]);
} else {
Map<String, String> map = new HashMap();
String[] kv = null;
String[] var4 = entrys;
int var5 = entrys.length;
for (int var6 = 0; var6 < var5; ++var6) {
String entry = var4[var6];
if (!isEmpty(entry)) {
kv = entry.split("=");
if (!isEmpty(kv)) {
if (kv.length > 1) {
map.put(kv[0], kv[1]);
} else {
map.put(kv[0], null);
}
}
}
}
return map;
map.put(kv[0], null);
}
}
return map;
}
public static String toHex(byte[] buf) {
/**
* 字符串转换方法 把字节数组转换成16进制字符串
*
* @param buf 初始字节数组
* @return 转换后字符串
*/
public static String toHex(byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
for (int i = 0; i < buf.length; ++i) {
if ((buf[i] & 255) < 16) {
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
}
strbuf.append(Long.toString((long) (buf[i] & 255), 16));
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
/**
* 获取字符串的MD5码
*
* @param str
* @return
*/
public static String toMD5(String str) {
if (str == null) {
return null;
} else {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException var3) {
var3.printStackTrace();
return null;
}
md.update(str.getBytes());
byte[] tmp = md.digest();
return toHex(tmp);
}
MessageDigest md = null;
try {
md = java.security.MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
md.update(str.getBytes());
byte tmp[] = md.digest();
return toHex(tmp);
}
/**
* SHA256加密
*
* @param str
* @return
*/
public static String toSHA256(final String str) {
return toSHA(str, "SHA-256");
}
/**
* SHA加密
*
* @param str
* @param key
* @return
*/
public static String toSHA(final String str, final String key) {
// 是否是有效字符串
if (str == null) {
return null;
}
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance(key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
messageDigest.update(str.getBytes());
byte tmp[] = messageDigest.digest();
return toHex(tmp);
}
/**
* 用ISO-8859-1解码 再用UFT-8编码
*
* @param str
* @return
*/
public static String iso2utf(String str) {
String utfStr = null;
try {
utfStr = new String(str.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException var3) {
var3.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return utfStr;
}
/**
* 判断字符串是否是数字
*
* @param str
* @return
*/
public static boolean isNumeric(String str) {
Pattern pattern = compile("[0-9]*");
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
return isNum.matches();
if (!isNum.matches()) {
return false;
}
return true;
}
/**
* 对ID32位进行编码
*
* @param id
* @return
*/
public static String codeId(String id) {
if (id != null && id.length() == 32) {
StringBuilder coded = new StringBuilder();
int i;
for (i = 0; i < 13; ++i) {
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
}
coded.append(id.substring(0, 11));
for (i = 0; i < 7; ++i) {
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
}
coded.append(id.substring(11));
for (i = 0; i < 12; ++i) {
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
}
return coded.toString();
} else {
if (id == null || id.length() != 32) {
return id;
}
StringBuilder coded = new StringBuilder();
for (int i = 0; i < 13; i++) {
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
}
coded.append(id.substring(0, 11));
for (int i = 0; i < 7; i++) {
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
}
coded.append(id.substring(11));
for (int i = 0; i < 12; i++) {
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
}
return coded.toString();
}
/**
* 对ID32位进行解码
*
* @param coded
* @return
*/
public static String decodeId(String coded) {
if (coded != null && coded.length() == 64) {
StringBuilder id = new StringBuilder();
id.append(coded.substring(13, 24));
id.append(coded.substring(31, 52));
return id.toString();
} else {
if (coded == null || coded.length() != 64) {
return coded;
}
StringBuilder id = new StringBuilder();
id.append(coded.substring(13, 24));
id.append(coded.substring(31, 52));
return id.toString();
}
/**
* map parameters 转url parameters
*
* @param sortedParams
* @return
*/
public static String getSignContent(Map<String, ?> sortedParams) {
StringBuffer content = new StringBuffer();
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);
Object value = sortedParams.get(key);
if (isNotEmpty(key) && isNotEmpty(value)) {
content.append((index == 0 ? "" : "&") + key + "=" + value);
++index;
}
}
return content.toString();
}
/**
* 替换手机号中间4位为*
@@ -192,4 +327,38 @@ public final class StrUtil {
public static String replaceMobile(String mobile) {
return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}
/**
* map 排序
*
* @param sortedParams
* @return
*/
public static Map<String, Object> mapSort(Map<String, ?> sortedParams) {
Map<String, Object> map = new HashMap<>();
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);
Object value = sortedParams.get(key);
map.put(key, value);
++index;
}
return map;
}
/**
* 设置Str带默认值
*
* @param msg
* @param defMsg
* @return
*/
public static String setStr(String msg, String defMsg) {
if (StrUtil.isEmpty(msg)) {
return defMsg;
}
return msg;
}
}

View File

@@ -1,5 +1,7 @@
package com.yexuejc.base.util;
import java.net.URL;
/**
* 系统工具类
*
@@ -22,4 +24,15 @@ public class SysUtils {
public static String getCachePath() {
return System.getProperty(PROJECT_ROOT_PATH);
}
/**
* 从根路径获取文件URL
*
* @param clazz
* @return
*/
public static URL getRootPath(Class clazz, String filePath) {
return clazz.getClass().getResource(StrUtil.setStr(filePath, "/"));
}
}