first commit
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
package top.ysoft.admin.common.util;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class ImageToBase64Utils {
|
||||
/**
|
||||
* 本地图片转base64
|
||||
*/
|
||||
public static String getImgFileToBase642(String imgFile) {
|
||||
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
|
||||
byte[] buffer = null;
|
||||
//读取图片字节数组
|
||||
try (InputStream inputStream = new FileInputStream(imgFile);) {
|
||||
int count = 0;
|
||||
while (count == 0) {
|
||||
count = inputStream.available();
|
||||
}
|
||||
buffer = new byte[count];
|
||||
inputStream.read(buffer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 对字节数组Base64编码
|
||||
return new String(Base64.encodeBase64(buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* 网络图片转base64
|
||||
*/
|
||||
public static String getImgUrlToBase64(String imgUrl) {
|
||||
byte[] buffer = null;
|
||||
InputStream inputStream = null;
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();) {
|
||||
// 创建URL
|
||||
URL url = new URL(imgUrl);
|
||||
// 创建链接
|
||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(5000);
|
||||
inputStream = conn.getInputStream();
|
||||
// 将内容读取内存中
|
||||
buffer = new byte[1024];
|
||||
int len = -1;
|
||||
while ((len = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, len);
|
||||
}
|
||||
buffer = outputStream.toByteArray();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
// 关闭inputStream流
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
// 对字节数组Base64编码
|
||||
return new String(Base64.encodeBase64(buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地或网络图片转base64
|
||||
*/
|
||||
public static String getImgStrToBase64(String imgStr) {
|
||||
InputStream inputStream = null;
|
||||
byte[] buffer = null;
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();) {
|
||||
//判断网络链接图片文件/本地目录图片文件
|
||||
if (imgStr.startsWith("http://") || imgStr.startsWith("https://")) {
|
||||
// 创建URL
|
||||
URL url = new URL(imgStr);
|
||||
// 创建链接
|
||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(5000);
|
||||
inputStream = conn.getInputStream();
|
||||
// 将内容读取内存中
|
||||
buffer = new byte[1024];
|
||||
int len = -1;
|
||||
while ((len = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, len);
|
||||
}
|
||||
buffer = outputStream.toByteArray();
|
||||
} else {
|
||||
inputStream = new FileInputStream(imgStr);
|
||||
int count = 0;
|
||||
while (count == 0) {
|
||||
count = inputStream.available();
|
||||
}
|
||||
buffer = new byte[count];
|
||||
inputStream.read(buffer);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
// 关闭inputStream流
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 对字节数组Base64编码
|
||||
|
||||
return new String(Base64.encodeBase64(buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对字节数组字符串进行Base64解码并生成图片
|
||||
*
|
||||
* @param imgStr 图片数据
|
||||
* @param imgFilePath 保存图片全路径地址
|
||||
* @return
|
||||
*/
|
||||
public static boolean generateImage(String imgStr, String imgFilePath) {
|
||||
//
|
||||
if (imgStr == null) //图像数据为空
|
||||
return false;
|
||||
try {
|
||||
//Base64解码
|
||||
byte[] b = Base64.decodeBase64(imgStr);
|
||||
for (int i = 0; i < b.length; ++i) {
|
||||
if (b[i] < 0) {//调整异常数据
|
||||
b[i] += 256;
|
||||
}
|
||||
}
|
||||
//生成jpeg图片
|
||||
OutputStream out = new FileOutputStream(imgFilePath);
|
||||
out.write(b);
|
||||
out.flush();
|
||||
out.close();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package top.ysoft.admin.common.util;
|
||||
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import top.continew.starter.core.exception.BusinessException;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
// java将图片的url转换成File,File转换成二进制流byte
|
||||
public class PictureUtils {
|
||||
//将Url转换为File
|
||||
public static File UrltoFile(String url) throws Exception {
|
||||
HttpURLConnection httpUrl = (HttpURLConnection)new URL(url).openConnection();
|
||||
httpUrl.connect();
|
||||
InputStream ins = httpUrl.getInputStream();
|
||||
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + "xie.jpg");
|
||||
if (file.exists()) {
|
||||
file.delete();//如果缓存中存在该文件就删除
|
||||
}
|
||||
OutputStream os = new FileOutputStream(file);
|
||||
int bytesRead;
|
||||
int len = 8192;
|
||||
byte[] buffer = new byte[len];
|
||||
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
|
||||
os.write(buffer, 0, bytesRead);
|
||||
}
|
||||
os.close();
|
||||
ins.close();
|
||||
return file;
|
||||
|
||||
}
|
||||
|
||||
//将File对象转换为byte[]的形式
|
||||
public static byte[] FileTobyte(File file) {
|
||||
FileInputStream fileInputStream = null;
|
||||
byte[] imgData = null;
|
||||
|
||||
try {
|
||||
|
||||
imgData = new byte[(int)file.length()];
|
||||
|
||||
//read file into bytes[]
|
||||
fileInputStream = new FileInputStream(file);
|
||||
fileInputStream.read(imgData);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fileInputStream != null) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return imgData;
|
||||
}
|
||||
|
||||
/**
|
||||
* url转变为 MultipartFile对象
|
||||
*
|
||||
* @param imageUrl 网络地址链接
|
||||
* @param fileName 文件名
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static MultipartFile createMultipartFile(String imageUrl, String fileName) {
|
||||
try {
|
||||
// 将在线图片地址转换为URL对象
|
||||
URL url = new URL(imageUrl);
|
||||
// 打开URL连接
|
||||
URLConnection connection = url.openConnection();
|
||||
// 转换为HttpURLConnection对象
|
||||
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
|
||||
// 获取输入流
|
||||
InputStream inputStream = httpURLConnection.getInputStream();
|
||||
// 读取输入流中的数据,并保存到字节数组中
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
byteArrayOutputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
// 将字节数组转换为字节数组
|
||||
byte[] bytes = byteArrayOutputStream.toByteArray();
|
||||
// 创建ByteArrayInputStream对象,将字节数组传递给它
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
||||
// 创建MultipartFile对象,将ByteArrayInputStream对象作为构造函数的参数
|
||||
return new MockMultipartFile(fileName, fileName + ".jpg", "image/jpg", byteArrayInputStream);
|
||||
} catch (IOException ex) {
|
||||
throw new BusinessException("附件无效");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件名获取contentType
|
||||
*/
|
||||
private static String getContentType(String fileName) {
|
||||
// 使用Java标准库的方法替代Hutool的StrUtil
|
||||
String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
|
||||
Map<String, String> contentTypeMap = new HashMap<>();
|
||||
contentTypeMap.put("jpg", "image/jpeg");
|
||||
contentTypeMap.put("jpeg", "image/jpeg");
|
||||
contentTypeMap.put("png", "image/png");
|
||||
contentTypeMap.put("gif", "image/gif");
|
||||
contentTypeMap.put("bmp", "image/bmp");
|
||||
// 可以根据需要添加更多的文件类型
|
||||
return contentTypeMap.getOrDefault(extension, "application/octet-stream");
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接从输入流压缩图片并转换为MultipartFile
|
||||
*
|
||||
* @param inputStream 图片输入流
|
||||
* @param originalFilename 原始文件名
|
||||
* @param quality 压缩质量 (0.0-1.0)
|
||||
* @return MultipartFile对象
|
||||
*/
|
||||
public static MultipartFile compressImage(InputStream inputStream,
|
||||
String originalFilename,
|
||||
float quality) throws IOException {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
try {
|
||||
// 直接将压缩后的图片写入内存流
|
||||
Thumbnails.of(inputStream).scale(1f).outputQuality(quality).toOutputStream(outputStream);
|
||||
|
||||
// 获取文件扩展名和contentType
|
||||
String extension = originalFilename.substring(originalFilename.lastIndexOf('.') + 1).toLowerCase();
|
||||
String contentType = getContentType(originalFilename);
|
||||
|
||||
// 创建MultipartFile对象
|
||||
byte[] compressedBytes = outputStream.toByteArray();
|
||||
ByteArrayInputStream input = new ByteArrayInputStream(compressedBytes);
|
||||
return new MockMultipartFile("file", originalFilename, contentType, input);
|
||||
} finally {
|
||||
// 确保流关闭
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package top.ysoft.admin.common.util;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import top.ysoft.admin.common.config.properties.RsaProperties;
|
||||
import top.continew.starter.core.exception.BusinessException;
|
||||
import top.continew.starter.core.validation.ValidationUtils;
|
||||
import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
|
||||
import top.continew.starter.security.crypto.encryptor.AesEncryptor;
|
||||
import top.continew.starter.security.crypto.encryptor.IEncryptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 加密/解密工具类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/21 21:41
|
||||
*/
|
||||
public class SecureUtils {
|
||||
|
||||
private SecureUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 公钥加密
|
||||
*
|
||||
* @param data 要加密的内容
|
||||
* @return 加密后的内容
|
||||
*/
|
||||
public static String encryptByRsaPublicKey(String data) {
|
||||
String publicKey = RsaProperties.PUBLIC_KEY;
|
||||
ValidationUtils.throwIfBlank(publicKey, "请配置 RSA 公钥");
|
||||
return encryptByRsaPublicKey(data, publicKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 私钥解密
|
||||
*
|
||||
* @param data 要解密的内容(Base64 加密过)
|
||||
* @return 解密后的内容
|
||||
*/
|
||||
public static String decryptByRsaPrivateKey(String data) {
|
||||
String privateKey = RsaProperties.PRIVATE_KEY;
|
||||
ValidationUtils.throwIfBlank(privateKey, "请配置 RSA 私钥");
|
||||
return decryptByRsaPrivateKey(data, privateKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 公钥加密
|
||||
*
|
||||
* @param data 要加密的内容
|
||||
* @param publicKey 公钥
|
||||
* @return 加密后的内容
|
||||
*/
|
||||
public static String encryptByRsaPublicKey(String data, String publicKey) {
|
||||
return new String(SecureUtil.rsa(null, publicKey).encrypt(data, KeyType.PublicKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 私钥解密
|
||||
*
|
||||
* @param data 要解密的内容(Base64 加密过)
|
||||
* @param privateKey 私钥
|
||||
* @return 解密后的内容
|
||||
*/
|
||||
public static String decryptByRsaPrivateKey(String data, String privateKey) {
|
||||
return new String(SecureUtil.rsa(privateKey, null).decrypt(Base64.decode(data), KeyType.PrivateKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对普通加密字段列表进行AES加密,优化starter加密模块后优化这个方法
|
||||
*
|
||||
* @param values 待加密内容
|
||||
* @return 加密后内容
|
||||
*/
|
||||
public static List<String> encryptFieldByAes(List<String> values) {
|
||||
IEncryptor encryptor = new AesEncryptor();
|
||||
CryptoProperties properties = SpringUtil.getBean(CryptoProperties.class);
|
||||
return values.stream().map(value -> {
|
||||
try {
|
||||
return encryptor.encrypt(value, properties.getPassword(), properties.getPublicKey());
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException("字段加密异常");
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user