base64上传
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.zg.common.utils;
|
||||
|
||||
import com.zg.framework.config.RuoYiConfig;
|
||||
import io.minio.*;
|
||||
import io.minio.errors.*;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -7,7 +8,11 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -92,6 +97,27 @@ public class MinioUtil {
|
||||
|
||||
return endpoint + "/" + bucketName + "/" + objectName;
|
||||
}
|
||||
public String uploadBase64(String imgStr,String bucketName,String folder){
|
||||
try {
|
||||
String objectName = folder + UUID.randomUUID().toString().replace("-", "") + ".png";
|
||||
String filePath = RuoYiConfig.getProfile()+"/"+bucketName+"/"+objectName;
|
||||
imgStr = imgStr.replace("data:image/png;base64,", "");
|
||||
// 解码Base64字符串
|
||||
byte[] imageBytes = Base64.getDecoder().decode(imgStr);
|
||||
|
||||
// 将字节转换为BufferedImage
|
||||
BufferedImage img = javax.imageio.ImageIO.read(new java.io.ByteArrayInputStream(imageBytes));
|
||||
|
||||
// 写入图片到文件系统
|
||||
File outputFile = new File(filePath);
|
||||
ImageIO.write(img, "PNG", outputFile);
|
||||
return endpoint + "/" + bucketName + "/" + objectName;
|
||||
// System.out.println("图片已保存到: " + outputFile.getAbsolutePath());
|
||||
} catch (Exception e) {
|
||||
System.err.println("转换或保存图片时发生错误: " + e.getMessage());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到默认 bucket 根目录
|
||||
|
||||
Reference in New Issue
Block a user