8 Commits

6 changed files with 63 additions and 17 deletions

3
.gitignore vendored
View File

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

View File

@@ -1,6 +1,12 @@
yexuejc-base 更新记录
------------------
#### 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/>

View File

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

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();
}