mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-11-19 06:57:45 +08:00
Compare commits
12 Commits
1.4.2
...
6ff859b4fb
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ff859b4fb | |||
| 8741562463 | |||
| b97fdf65bc | |||
| 3987f982be | |||
| 556f2a49d2 | |||
| 3b0cdc832b | |||
| 5a96c23882 | |||
| d602d4370e | |||
| 471da3dbcb | |||
| 58d713e025 | |||
| 75f7aaeb77 | |||
| a7132e9b5b |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -22,4 +22,5 @@ build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
||||
.nb-gradle/
|
||||
*.iml~
|
||||
|
||||
@@ -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
|
||||
```
|
||||
|
||||
25
UPDATE.md
25
UPDATE.md
@@ -1,6 +1,31 @@
|
||||
yexuejc-base 更新记录
|
||||
------------------
|
||||
|
||||
#### version :1.4.5
|
||||
**time:2022-5-9 13:37:31** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. 扩展FileUtil,优化Base64的包
|
||||
#
|
||||
#### version :1.4.4
|
||||
**time:2021-4-24 00:41:31** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. JsonUtil类修复格式化出现的时区差:现使用TimeZone.getDefault()时区
|
||||
#
|
||||
#### version :1.4.3
|
||||
**time:2021-2-6 11:42:49** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. DateTimeUtil类增加时间格式
|
||||
#
|
||||
#### version :1.4.2
|
||||
**time:2021-2-3 11:40:11** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. 优化时间转换格式
|
||||
>2. 增加注解 ToUeProperty 转换时间为时间戳的功能
|
||||
#
|
||||
#### version :1.4.1
|
||||
**time:2021-1-31 12:59:24** <br/>
|
||||
**branch:** master <br/>
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -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>
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
36
src/test/java/com/yexuejc/base/util/JsonUtilTest.java
Normal file
36
src/test/java/com/yexuejc/base/util/JsonUtilTest.java
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user