feat(基础功能增强):
新增两个base64工具: 1、检查base64字符串是否是图片 2、将网络图片转换为Base64字符串
This commit is contained in:
parent
87cb92cf1b
commit
21bd3b843b
|
@ -1,5 +1,17 @@
|
|||
package com.ruoyi.common.core.utils.sign;
|
||||
|
||||
import com.ruoyi.common.core.exception.ServiceException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Base64工具类
|
||||
*
|
||||
|
@ -7,6 +19,7 @@ package com.ruoyi.common.core.utils.sign;
|
|||
*/
|
||||
public final class Base64
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Base64.class);
|
||||
static private final int BASELENGTH = 128;
|
||||
static private final int LOOKUPLENGTH = 64;
|
||||
static private final int TWENTYFOURBITGROUP = 24;
|
||||
|
@ -288,4 +301,80 @@ public final class Base64
|
|||
}
|
||||
return newSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查base64字符串是否是图片
|
||||
*
|
||||
* @param base64 base64字符串
|
||||
* @return 是否为图片
|
||||
*/
|
||||
public static boolean checkBase64IsImage(String base64) {
|
||||
try {
|
||||
// 将 data:image/jpg;base64, 去掉
|
||||
int i = base64.indexOf(",");
|
||||
base64 = base64.substring(i + 1);
|
||||
// 图片内容 base64 解密。
|
||||
byte[] data = Base64.decode(base64); // 校验是否是图片格式
|
||||
InputStream inputStream = new ByteArrayInputStream(data);
|
||||
BufferedImage image = ImageIO.read(inputStream);
|
||||
// 如果成功创建BufferedImage,且其宽度和高度大于0,则认为是图片
|
||||
return image != null && image.getWidth() > 0 && image.getHeight() > 0;
|
||||
} catch (Exception e) {
|
||||
logger.error("校验base64是否为图片失败:{}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将网络图片转换为Base64字符串
|
||||
*
|
||||
* @param imageUrl 图片地址
|
||||
* @return 图片base64字符串
|
||||
*/
|
||||
public static String imageUrlToBase64(String imageUrl) {
|
||||
|
||||
try {
|
||||
// 从URL获取图片输入流
|
||||
URL url = new URL(imageUrl);
|
||||
InputStream inputStream = url.openStream();
|
||||
|
||||
// 读取图片输入流并转换为字节数组
|
||||
byte[] imageBytes = readAllBytes(inputStream);
|
||||
|
||||
// 获取图片MIME类型
|
||||
String mimeType = getImageMimeType(imageUrl);
|
||||
String base64 = java.util.Base64.getEncoder().encodeToString(imageBytes);
|
||||
if (!checkBase64IsImage(base64)) {
|
||||
throw new ServiceException("上传的文件非图片类型");
|
||||
}
|
||||
;
|
||||
// 添加MIME类型前缀
|
||||
return "data:" + mimeType + ";base64," + base64;
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("图片地址错误");
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] readAllBytes(InputStream inputStream) throws IOException {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
byteArrayOutputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
private static String getImageMimeType(String imageUrl) throws IOException {
|
||||
// 根据URL获取图片MIME类型
|
||||
URL url = new URL(imageUrl);
|
||||
InputStream inputStream = url.openStream();
|
||||
String mimeType = java.net.URLConnection.guessContentTypeFromStream(inputStream);
|
||||
if (mimeType == null) {
|
||||
return "image/jpeg";
|
||||
} else {
|
||||
return mimeType;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue