mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-08-03 17:29:30 +08:00
Compare commits
7 Commits
3b0cdc832b
...
6ff859b4fb
Author | SHA1 | Date | |
---|---|---|---|
6ff859b4fb | |||
8741562463 | |||
b97fdf65bc | |||
3987f982be | |||
556f2a49d2 | |||
75f7aaeb77 | |||
a7132e9b5b |
@ -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
|
pom.xml
|
||||||
```
|
```
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
yexuejc-base 更新记录
|
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
|
#### version :1.4.4
|
||||||
**time:2021-4-24 00:41:31** <br/>
|
**time:2021-4-24 00:41:31** <br/>
|
||||||
**branch:** master <br/>
|
**branch:** master <br/>
|
||||||
|
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>top.yexuejc</groupId>
|
<groupId>top.yexuejc</groupId>
|
||||||
<artifactId>yexuejc-base</artifactId>
|
<artifactId>yexuejc-base</artifactId>
|
||||||
<version>1.4.4</version>
|
<version>1.4.5</version>
|
||||||
|
|
||||||
<name>${project.artifactId}</name>
|
<name>${project.artifactId}</name>
|
||||||
<url>https://github.com/yexuejc/yexuejc-base</url>
|
<url>https://github.com/yexuejc/yexuejc-base</url>
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package com.yexuejc.base.util;
|
package com.yexuejc.base.util;
|
||||||
|
|
||||||
import sun.misc.BASE64Encoder;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.nio.MappedByteBuffer;
|
import java.nio.MappedByteBuffer;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.zip.CRC32;
|
import java.util.zip.CRC32;
|
||||||
|
|
||||||
@ -286,7 +285,17 @@ public class FileUtil {
|
|||||||
* @param file
|
* @param file
|
||||||
* @return
|
* @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;
|
FileInputStream fileInputStream = null;
|
||||||
byte[] data = null;
|
byte[] data = null;
|
||||||
// 读取图片字节数组
|
// 读取图片字节数组
|
||||||
@ -300,8 +309,22 @@ public class FileUtil {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
// 对字节数组Base64编码
|
// 对字节数组Base64编码
|
||||||
BASE64Encoder encoder = new BASE64Encoder();
|
return Base64.getEncoder().encode(data);
|
||||||
return encoder.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;
|
package com.yexuejc.base.util;
|
||||||
|
|
||||||
import sun.misc.BASE64Decoder;
|
|
||||||
import sun.misc.BASE64Encoder;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageReader;
|
import javax.imageio.ImageReader;
|
||||||
@ -11,6 +9,7 @@ import java.net.HttpURLConnection;
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,8 +46,7 @@ public class ImgUtil {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
// 对字节数组Base64编码
|
// 对字节数组Base64编码
|
||||||
BASE64Encoder encoder = new BASE64Encoder();
|
return new String(Base64.getEncoder().encode(data.toByteArray()));
|
||||||
return encoder.encode(data.toByteArray());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -237,7 +235,7 @@ public class ImgUtil {
|
|||||||
* @return String 编码后的字符串
|
* @return String 编码后的字符串
|
||||||
*/
|
*/
|
||||||
public static String encode(byte[] bytes) {
|
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
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static byte[] decode(String encodeStr) throws IOException {
|
public static byte[] decode(String encodeStr) throws IOException {
|
||||||
byte[] bt = null;
|
return Base64.getDecoder().decode(encodeStr);
|
||||||
BASE64Decoder decoder = new BASE64Decoder();
|
|
||||||
bt = decoder.decodeBuffer(encodeStr);
|
|
||||||
return bt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user