12 Commits
1.4.2 ... 1.4.5

Author SHA1 Message Date
6ff859b4fb 1.4.5 2022-05-09 13:44:53 +08:00
8741562463 扩展FileUtil 2022-05-08 19:30:08 +08:00
b97fdf65bc 变更Base64使用包,使其兼容openjdk11 2022-05-08 16:53:23 +08:00
3987f982be 变更Base64使用包,使其兼容openjdk11 2022-05-08 16:43:03 +08:00
556f2a49d2 Merge branch 'master' into develop 2022-05-08 13:56:27 +08:00
3b0cdc832b update 忽略文件 2022-05-08 13:52:23 +08:00
5a96c23882 1.4.4 JsonUtil类修复格式化出现的时区差 2021-04-24 00:52:35 +08:00
d602d4370e pom 1.4.3 2021-02-06 11:44:54 +08:00
471da3dbcb 提交文档 2021-02-06 11:43:45 +08:00
58d713e025 1.4.3
DateTimeUtil 增加时间格式
2021-02-06 11:41:51 +08:00
75f7aaeb77 Merge branch 'master' into develop 2019-01-06 18:34:40 +08:00
a7132e9b5b Merge pull request #1 from yexuejc/master
1.1.7
2018-08-17 11:47:09 +08:00
9 changed files with 120 additions and 18 deletions

3
.gitignore vendored
View File

@@ -22,4 +22,5 @@ build/
nbbuild/
dist/
nbdist/
.nb-gradle/
.nb-gradle/
*.iml~

View File

@@ -23,7 +23,7 @@ gitee:https://gitee.com/jzsw-it/yexuejc-base
### 使用
>yexuejc.base.version=1.4.1
>yexuejc.base.version=1.4.5
pom.xml
```

View File

@@ -1,6 +1,31 @@
yexuejc-base 更新记录
------------------
#### version 1.4.5
**time2022-5-9 13:37:31** <br/>
**branch** master <br/>
**update** <br/>
>1. 扩展FileUtil,优化Base64的包
#
#### version 1.4.4
**time2021-4-24 00:41:31** <br/>
**branch** master <br/>
**update** <br/>
>1. JsonUtil类修复格式化出现的时区差现使用TimeZone.getDefault()时区
#
#### version 1.4.3
**time2021-2-6 11:42:49** <br/>
**branch** master <br/>
**update** <br/>
>1. DateTimeUtil类增加时间格式
#
#### version 1.4.2
**time2021-2-3 11:40:11** <br/>
**branch** master <br/>
**update** <br/>
>1. 优化时间转换格式
>2. 增加注解 ToUeProperty 转换时间为时间戳的功能
#
#### version 1.4.1
**time2021-1-31 12:59:24** <br/>
**branch** master <br/>

View File

@@ -6,7 +6,7 @@
<groupId>top.yexuejc</groupId>
<artifactId>yexuejc-base</artifactId>
<version>1.4.2</version>
<version>1.4.5</version>
<name>${project.artifactId}</name>
<url>https://github.com/yexuejc/yexuejc-base</url>

View File

@@ -14,6 +14,7 @@ import java.util.Date;
*/
public class DateTimeUtil {
public static String DATE_PATTERN = "yyyy-MM-dd";
public static String TIME_PATTERN = "HH:mm:ss";
public static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static String DATE_TIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

View File

@@ -1,13 +1,12 @@
package com.yexuejc.base.util;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.logging.Logger;
import java.util.zip.CRC32;
@@ -286,7 +285,17 @@ public class FileUtil {
* @param file
* @return
*/
public static String base64(File file) {
public static String base64ToStr(File file) {
return new String(base64(file));
}
/**
* 获取文件base64
*
* @param file
* @return
*/
public static byte[] base64(File file) {
FileInputStream fileInputStream = null;
byte[] data = null;
// 读取图片字节数组
@@ -300,8 +309,22 @@ public class FileUtil {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
return Base64.getEncoder().encode(data);
}
/**
* base64转文件
* <p>
* <i>
* 文件转base64请使用 {@link FileUtil#base64(File)}
* </i>
*
* @param decode {@link FileUtil#base64ToStr(File)} 的结果
* @param fileName 文件名称(包含路径)
* @return 返回保存地址
*/
public static String base64ToFile(String decode, String fileName) {
return base64ToFile(Base64.getDecoder().decode(decode.getBytes()), fileName);
}
/**

View File

@@ -1,7 +1,5 @@
package com.yexuejc.base.util;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
@@ -11,6 +9,7 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.Base64;
import java.util.Iterator;
/**
@@ -47,8 +46,7 @@ public class ImgUtil {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data.toByteArray());
return new String(Base64.getEncoder().encode(data.toByteArray()));
}
/**
@@ -237,7 +235,7 @@ public class ImgUtil {
* @return String 编码后的字符串
*/
public static String encode(byte[] bytes) {
return new BASE64Encoder().encode(bytes);
return new String(Base64.getEncoder().encode(bytes));
}
/**
@@ -248,10 +246,7 @@ public class ImgUtil {
* @throws IOException
*/
public static byte[] decode(String encodeStr) throws IOException {
byte[] bt = null;
BASE64Decoder decoder = new BASE64Decoder();
bt = decoder.decodeBuffer(encodeStr);
return bt;
return Base64.getDecoder().decode(encodeStr);
}
/**

View File

@@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.type.MapType;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TimeZone;
import java.util.logging.Logger;
/**
@@ -46,6 +47,8 @@ public class JsonUtil {
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//设置一下时区,可以和程序同步避免时区问题
objectMapper.setTimeZone(TimeZone.getDefault());
objectMapper.setDateFormat(DateUtil.DATE_TIME_FORMAT);
}
@@ -55,6 +58,15 @@ public class JsonUtil {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
}
/**
* 这个设置不能改变JsonUtil自带的objectMapper设置只能修改传入objMapper的设置
* @param objMapper
*/
public static void initSnakeCase(ObjectMapper objMapper) {
//驼峰下划线互转
objMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
}
/**
* 每调用一次生成一个全新的ObjectMapper供特殊场景使用与通用ObjectMapper没有关系
*
@@ -66,6 +78,15 @@ public class JsonUtil {
return objectMapper;
}
/**
* 返回 ObjectMapper对象供外部设置特定参数
*
* @return
*/
public static ObjectMapper getObjectMapper() {
return objectMapper;
}
/**
* 将json转换为某个类
*
@@ -106,7 +127,7 @@ public class JsonUtil {
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
} catch (IOException e) {
log.warning("json to Object IOException.\n" + e.getMessage());
}catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
}

View File

@@ -0,0 +1,36 @@
package com.yexuejc.base.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class JsonUtilTest {
public static void main(String[] args) {
TestA testA = new TestA("张三", 1, false);
System.out.println(JsonUtil.json2Obj(JsonUtil.obj2Json(testA), Map.class));
List<TestA> list = new ArrayList<>();
list.add(testA);
list.add(testA);
list.add(testA);
System.out.println(JsonUtil.json2Obj(JsonUtil.obj2Json(list), List.class,TestA.class));
}
static class TestA implements Serializable {
public String name;
public Integer id;
public Boolean sex;
public TestA() {
}
public TestA(String name, Integer id, Boolean sex) {
this.name = name;
this.id = id;
this.sex = sex;
}
}
}