This commit is contained in:
parent
fb37f83e5b
commit
3b2aac8602
|
|
@ -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("资源表不能为空");
|
||||||
|
|
|
||||||
|
|
@ -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;
|
String suffix=null;
|
||||||
if(bast64.contains(",")){
|
if(bast64.contains(",")){
|
||||||
String de = bast64.split(",")[1];
|
// 2. 解析Base64前缀,获取图片格式(如png、jpg)
|
||||||
byte[] decodedBytes = Base64.getDecoder().decode(de);
|
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;
|
int fileSizeInBytes = decodedBytes.length;
|
||||||
long size = (long) Math.ceil(fileSizeInBytes / 1024.0); // 向上取整并保持long类型
|
long size = (long) Math.ceil(fileSizeInBytes / 1024.0); // 向上取整并保持long类型
|
||||||
vo.setFileSize(size);
|
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();
|
||||||
|
if(StringUtils.isEmpty(suffix)){
|
||||||
suffix=getBase64Type(bast64);
|
suffix=getBase64Type(bast64);
|
||||||
|
}
|
||||||
|
String fileName=uuid+"."+suffix;
|
||||||
String fileName=uuid+".jpeg";
|
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue