This commit is contained in:
haozq 2025-09-26 14:55:55 +08:00
parent fb37f83e5b
commit 3b2aac8602
3 changed files with 29 additions and 17 deletions

View File

@ -68,7 +68,7 @@ public class FileUtilController {
* @return * @return
*/ */
@PostMapping("uploadBast64") @PostMapping("uploadBast64")
public R<UploadFileVo> upload(@RequestBody FileVo vo) { public R<UploadFileVo> uploadBast64(@RequestBody FileVo vo) {
try { try {
if (ObjectUtil.isEmpty(vo.getSourceTable())) { if (ObjectUtil.isEmpty(vo.getSourceTable())) {
return R.fail("资源表不能为空"); return R.fail("资源表不能为空");

View File

@ -34,17 +34,25 @@ public class FileUtilsServiceImpl {
private FileUtilMapper mapper; private FileUtilMapper mapper;
public UploadFileVo upload(String sourceTable, String sourceId, String sourceType,String prefix,String bast64,String bucketName) { public UploadFileVo upload(String sourceTable, String sourceId, String sourceType,String prefix,String bast64,String bucketName) {
UploadFileVo vo=new UploadFileVo();
String suffix;
if(bast64.contains(",")){
String de = bast64.split(",")[1];
byte[] decodedBytes = Base64.getDecoder().decode(de);
// 计算文件大小
int fileSizeInBytes = decodedBytes.length;
long size = (long) Math.ceil(fileSizeInBytes / 1024.0); // 向上取整并保持long类型
vo.setFileSize(size);
}
String suffix=null;
if(bast64.contains(",")){
// 2. 解析Base64前缀获取图片格式如pngjpg
String imgPrefix = bast64.substring(0, bast64.indexOf(";base64,"));
if (StringUtils.isEmpty(suffix)) {
suffix = imgPrefix.substring(imgPrefix.lastIndexOf("/") + 1); // 从前缀提取后缀如png
}
//保留纯净的bast64
bast64 = bast64.split(",")[1];
}
UploadFileVo vo=new UploadFileVo();
//计算文件大小
byte[] decodedBytes = Base64.getDecoder().decode(bast64);
// 计算文件大小
int fileSizeInBytes = decodedBytes.length;
long size = (long) Math.ceil(fileSizeInBytes / 1024.0); // 向上取整并保持long类型
vo.setFileSize(size);
vo.setSourceId(sourceId); vo.setSourceId(sourceId);
vo.setSourceTable(sourceTable); vo.setSourceTable(sourceTable);
vo.setSourceType(sourceType); vo.setSourceType(sourceType);
@ -56,10 +64,10 @@ public class FileUtilsServiceImpl {
String month=DateUtils.getCurrentMonth(); String month=DateUtils.getCurrentMonth();
String year=DateUtils.getCurrentYear(); String year=DateUtils.getCurrentYear();
String uuid = StringUtils.randomUUID(); String uuid = StringUtils.randomUUID();
suffix=getBase64Type(bast64); if(StringUtils.isEmpty(suffix)){
suffix=getBase64Type(bast64);
}
String fileName=uuid+".jpeg"; String fileName=uuid+"."+suffix;
String filePath=prefix+"/"+year+"/"+month+"/"+day+"/"+fileName; String filePath=prefix+"/"+year+"/"+month+"/"+day+"/"+fileName;
vo.setFilePath(filePath); vo.setFilePath(filePath);
vo.setOriginFileName(fileName); vo.setOriginFileName(fileName);

View File

@ -26,6 +26,8 @@ import java.util.concurrent.Executors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import static java.util.Base64.*;
/** /**
* MinioUtil 工具类 * MinioUtil 工具类
* 封装 MinIO 常用操作如文件上传下载删除复制等功能 * 封装 MinIO 常用操作如文件上传下载删除复制等功能
@ -307,7 +309,7 @@ public class MinioUtil {
} }
byte[] bytes = outputStream.toByteArray(); byte[] bytes = outputStream.toByteArray();
String bast64= Base64.getEncoder().encodeToString(bytes); String bast64= getEncoder().encodeToString(bytes);
inputStream.close(); inputStream.close();
outputStream.close(); outputStream.close();
return bast64; return bast64;
@ -357,7 +359,9 @@ public class MinioUtil {
public static InputStream base64ToInputStream(String base64) { public static InputStream base64ToInputStream(String base64) {
ByteArrayInputStream stream = null; ByteArrayInputStream stream = null;
try { try {
byte[] bytes = Base64.getEncoder().encode(base64.trim().getBytes()); // InputStream inputStream = new ByteArrayInputStream(imgBytes)
byte[] bytes = Base64.getDecoder().decode(base64.trim().getBytes());
// byte[] imgBytes = Base64.decodeBase64(base64);
stream = new ByteArrayInputStream(bytes); stream = new ByteArrayInputStream(bytes);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();