Merge branch 'dev_minio_20251210' into master
This commit is contained in:
@@ -180,6 +180,12 @@
|
||||
<version>3.3.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>8.5.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
package minio;
|
||||
|
||||
import com.aliyun.oss.ServiceException;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MinIoUploadService {
|
||||
@Autowired
|
||||
private MinioClient minioClient;
|
||||
private static String bucket="ngwl";
|
||||
private static String host="http://10.102.192.5:6000";
|
||||
private static String accessKey="minioadmin";
|
||||
private static String secretKey="gmminioadmin";
|
||||
/**
|
||||
* minio 工具客户端
|
||||
*/
|
||||
private static MinioClient minioClient1 = null;
|
||||
|
||||
private static String FILE_URL;
|
||||
|
||||
/**
|
||||
* 上传到minio
|
||||
* @param fileName
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
public String uploadFileToMinIo(MultipartFile multipartFile) {
|
||||
try {
|
||||
initMinio();
|
||||
InputStream inputStream = multipartFile.getInputStream();
|
||||
|
||||
// 上传文件的名称
|
||||
String filename = multipartFile.getOriginalFilename();
|
||||
|
||||
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
|
||||
// PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
|
||||
// // 文件的ContentType
|
||||
// putObjectOptions.setContentType(multipartFile.getContentType());
|
||||
|
||||
String prefix = filename.substring(filename.lastIndexOf(".") + 1);//后缀
|
||||
//根据当前时间生成文件夹
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Date time = calendar.getTime();
|
||||
// 2. 格式化系统时间--- 这里的时间格式可以根据自己的需要进行改变
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String buckfilename = format.format(time);
|
||||
|
||||
String uuid= UUID.randomUUID().toString().replace("-","");//customer_YYYYMMDDHHMM
|
||||
String fileName = buckfilename+"/"+uuid+"."+prefix; //文件全路径
|
||||
|
||||
PutObjectArgs objectArgs = PutObjectArgs.builder()
|
||||
.object(fileName).bucket(bucket)
|
||||
.stream(inputStream,inputStream.available(),-1).build();
|
||||
//文件名称相同会覆盖
|
||||
minioClient.putObject(objectArgs);
|
||||
String url =host+ "/"+bucket + "/"+fileName;
|
||||
|
||||
//
|
||||
return url;
|
||||
}catch (Exception e){
|
||||
//log.error(e.getMessage(),e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
private MinioClient initMinio() {
|
||||
try{
|
||||
MinioClient minioClient = MinioClient.builder()
|
||||
.endpoint(host)
|
||||
.credentials(accessKey, secretKey)
|
||||
.build();
|
||||
return minioClient;
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return minioClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* 文件上传成功,返回文件完整访问路径
|
||||
* 文件上传失败,返回 null
|
||||
*
|
||||
* @param multipartFile 待上传文件
|
||||
* @param fileDir 文件保存目录
|
||||
* @return oss 中的相对文件路径
|
||||
*/
|
||||
/* public static String upload(MultipartFile multipartFile, String fileDir) {
|
||||
|
||||
|
||||
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||
//初始化minio
|
||||
initMinio();
|
||||
// bucket 不存在,创建
|
||||
if (!minioClient1.bucketExists(bucket)) {
|
||||
minioClient1.makeBucket(bucket);
|
||||
}
|
||||
// 上传文件的名称
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
|
||||
// PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
|
||||
// // 文件的ContentType
|
||||
// putObjectOptions.setContentType(multipartFile.getContentType());
|
||||
|
||||
SimpleDateFormat sdYear = new SimpleDateFormat("yyyy");
|
||||
SimpleDateFormat sdMonth = new SimpleDateFormat("MM");
|
||||
SimpleDateFormat sdDay = new SimpleDateFormat("dd");
|
||||
String year = sdYear.format(new Date());
|
||||
String month = sdMonth.format(new Date());
|
||||
String day = sdDay.format(new Date());
|
||||
String filrDir = "wlhy/" + year + "/" + month + "/" + day + "/";
|
||||
|
||||
// minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, putObjectOptions);
|
||||
minioClient1.putObject(bucket, filrDir+timestamp+fileName, inputStream, multipartFile.getContentType());
|
||||
// 返回访问路径
|
||||
String fileurl = host+ "/"+bucket + "/"+filrDir+timestamp+ UriUtils.encode(fileName, StandardCharsets.UTF_8);
|
||||
System.out.println("fileurl===="+fileurl);
|
||||
FILE_URL = fileurl;
|
||||
return fileurl;
|
||||
} catch (ServiceException e){
|
||||
log.error(e.getErrorMessage());
|
||||
} catch (Exception e){
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return FILE_URL;
|
||||
}*/
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
package minio;
|
||||
|
||||
import com.aliyun.oss.ServiceException;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.errors.InvalidEndpointException;
|
||||
import io.minio.errors.InvalidPortException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
import org.jeecg.modules.oss.dto.WaterMarkDTO;
|
||||
import org.jeecg.modules.system.entity.SysDepartInterfaceInfo;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description: minio oss 上传工具类
|
||||
*/
|
||||
@Slf4j
|
||||
public class MinioBootUtil {
|
||||
|
||||
private static String bucket;
|
||||
private static String host;
|
||||
private static String accessKey;
|
||||
private static String secretKey;
|
||||
/**
|
||||
* minio 工具客户端
|
||||
*/
|
||||
private static MinioClient minioClient = null;
|
||||
|
||||
private static String FILE_URL;
|
||||
|
||||
/**
|
||||
* @Description 强制初始化minio
|
||||
* @Author Alex
|
||||
* @Date 2023/10/25 11:12
|
||||
*/
|
||||
public static MinioClient forceInitMinio(List<SysDepartInterfaceInfo> sysDepartInterfaceInfoList) throws InvalidPortException, InvalidEndpointException {
|
||||
for (SysDepartInterfaceInfo sysDepartInterfaceInfo : sysDepartInterfaceInfoList) {
|
||||
String interfaceTypeKey = sysDepartInterfaceInfo.getInterfaceTypeKey();//接口参数
|
||||
String interfaceTypeValue = sysDepartInterfaceInfo.getInterfaceTypeValue();//接口参数值
|
||||
switch (interfaceTypeKey) {
|
||||
case "bucket":
|
||||
bucket = interfaceTypeValue;
|
||||
break;
|
||||
case "host":
|
||||
host = interfaceTypeValue;
|
||||
break;
|
||||
case "accessKey":
|
||||
accessKey = interfaceTypeValue;
|
||||
break;
|
||||
case "secretKey":
|
||||
secretKey = interfaceTypeValue;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
minioClient = new MinioClient(host, accessKey, secretKey);
|
||||
return minioClient;
|
||||
}
|
||||
private static MinioClient initMinio() throws InvalidPortException, InvalidEndpointException {
|
||||
if (minioClient == null) {
|
||||
minioClient = new MinioClient(host, accessKey, secretKey);
|
||||
}
|
||||
return minioClient;
|
||||
}
|
||||
/**
|
||||
* 上传文件
|
||||
* 文件上传成功,返回文件完整访问路径
|
||||
* 文件上传失败,返回 null
|
||||
*
|
||||
* @param multipartFile 待上传文件
|
||||
* @param fileDir 文件保存目录
|
||||
* @return oss 中的相对文件路径
|
||||
*/
|
||||
public static String upload(MultipartFile multipartFile, String fileDir, List<SysDepartInterfaceInfo> sysDepartInterfaceInfoList) {
|
||||
for (SysDepartInterfaceInfo sysDepartInterfaceInfo : sysDepartInterfaceInfoList) {
|
||||
String interfaceTypeKey = sysDepartInterfaceInfo.getInterfaceTypeKey();//接口参数
|
||||
String interfaceTypeValue = sysDepartInterfaceInfo.getInterfaceTypeValue();//接口参数值
|
||||
switch (interfaceTypeKey) {
|
||||
case "bucket":
|
||||
bucket = interfaceTypeValue;
|
||||
break;
|
||||
case "host":
|
||||
host = interfaceTypeValue;
|
||||
break;
|
||||
case "accessKey":
|
||||
accessKey = interfaceTypeValue;
|
||||
break;
|
||||
case "secretKey":
|
||||
secretKey = interfaceTypeValue;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||
//初始化minio
|
||||
initMinio();
|
||||
// bucket 不存在,创建
|
||||
if (!minioClient.bucketExists(bucket)) {
|
||||
minioClient.makeBucket(bucket);
|
||||
}
|
||||
// 上传文件的名称
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
|
||||
// PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
|
||||
// // 文件的ContentType
|
||||
// putObjectOptions.setContentType(multipartFile.getContentType());
|
||||
|
||||
SimpleDateFormat sdYear = new SimpleDateFormat("yyyy");
|
||||
SimpleDateFormat sdMonth = new SimpleDateFormat("MM");
|
||||
SimpleDateFormat sdDay = new SimpleDateFormat("dd");
|
||||
String year = sdYear.format(new Date());
|
||||
String month = sdMonth.format(new Date());
|
||||
String day = sdDay.format(new Date());
|
||||
String filrDir = "wlhy/" + year + "/" + month + "/" + day + "/";
|
||||
|
||||
// minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, putObjectOptions);
|
||||
minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, multipartFile.getContentType());
|
||||
// 返回访问路径
|
||||
String fileurl = host+ "/"+bucket + "/"+filrDir+timestamp+ UriUtils.encode(fileName, StandardCharsets.UTF_8);
|
||||
System.out.println("fileurl===="+fileurl);
|
||||
FILE_URL = fileurl;
|
||||
return fileurl;
|
||||
} catch (ServiceException e){
|
||||
log.error(e.getErrorMessage());
|
||||
} catch (Exception e){
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return FILE_URL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件 带时间水印
|
||||
* 文件上传成功,返回文件完整访问路径
|
||||
* 文件上传失败,返回 null
|
||||
*
|
||||
* @param multipartFile 待上传文件
|
||||
* @param fileDir 文件保存目录
|
||||
* @return oss 中的相对文件路径
|
||||
*/
|
||||
public static String uploadWaterMark(MultipartFile multipartFile, WaterMarkDTO waterMarkDTO, String fileDir, List<SysDepartInterfaceInfo> sysDepartInterfaceInfoList) {
|
||||
for (SysDepartInterfaceInfo sysDepartInterfaceInfo : sysDepartInterfaceInfoList) {
|
||||
String interfaceTypeKey = sysDepartInterfaceInfo.getInterfaceTypeKey();//接口参数
|
||||
String interfaceTypeValue = sysDepartInterfaceInfo.getInterfaceTypeValue();//接口参数值
|
||||
switch (interfaceTypeKey) {
|
||||
case "bucket":
|
||||
bucket = interfaceTypeValue;
|
||||
break;
|
||||
case "host":
|
||||
host = interfaceTypeValue;
|
||||
break;
|
||||
case "accessKey":
|
||||
accessKey = interfaceTypeValue;
|
||||
break;
|
||||
case "secretKey":
|
||||
secretKey = interfaceTypeValue;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 压缩代码
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
//初始化minio
|
||||
initMinio();
|
||||
// bucket 不存在,创建
|
||||
if (!minioClient.bucketExists(bucket)) {
|
||||
minioClient.makeBucket(bucket);
|
||||
}
|
||||
String OriginalFilename = multipartFile.getOriginalFilename();
|
||||
inputStream = multipartFile.getInputStream();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
//水印内容
|
||||
String waterMarkContent = sdf.format(new Date());
|
||||
//以下两种方法加水印
|
||||
//方法1
|
||||
if (OriginalFilename.indexOf(".pdf") < 0) {
|
||||
ByteArrayOutputStream byteArrayOutputStreamut = new ByteArrayOutputStream();
|
||||
Thumbnails.of(inputStream).width(1024).keepAspectRatio(true).outputQuality(0.9).toOutputStream(byteArrayOutputStreamut);
|
||||
inputStream = new ByteArrayInputStream(byteArrayOutputStreamut.toByteArray());
|
||||
//添加水印
|
||||
inputStream = OssBootUtil.buildWatermarkOfText(inputStream, waterMarkDTO, OssBootUtil.WaterMarkLocation.SOUTH_WEST, null, 0.5f);
|
||||
}
|
||||
//方法2
|
||||
/*if (ImageUtil.isImage(multipartFile.getOriginalFilename())) {
|
||||
inputStream = ImageUtil.getInputStream(
|
||||
ImageUtil.setWatermark(
|
||||
ImageUtil.compress(
|
||||
ImageIO.read(inputStream)),waterMarkContent),
|
||||
ImageUtil.getFileExtention(multipartFile.getOriginalFilename()));
|
||||
}*/
|
||||
StringBuilder fileUrl = new StringBuilder();
|
||||
String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf('.'));
|
||||
// 上传文件的名称
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
|
||||
// PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
|
||||
// // 文件的ContentType
|
||||
// putObjectOptions.setContentType(multipartFile.getContentType());
|
||||
|
||||
SimpleDateFormat sdYear = new SimpleDateFormat("yyyy");
|
||||
SimpleDateFormat sdMonth = new SimpleDateFormat("MM");
|
||||
SimpleDateFormat sdDay = new SimpleDateFormat("dd");
|
||||
String year = sdYear.format(new Date());
|
||||
String month = sdMonth.format(new Date());
|
||||
String day = sdDay.format(new Date());
|
||||
String filrDir = "wlhy/" + year + "/" + month + "/" + day + "/";
|
||||
|
||||
// minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, putObjectOptions);
|
||||
minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, multipartFile.getContentType());
|
||||
// 返回访问路径
|
||||
String fileurl = host+ "/"+bucket + "/"+filrDir+timestamp+ UriUtils.encode(fileName, StandardCharsets.UTF_8);
|
||||
System.out.println("fileurl===="+fileurl);
|
||||
FILE_URL = fileurl;
|
||||
return fileurl;
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return FILE_URL;
|
||||
}
|
||||
|
||||
}
|
||||
+17
-1
@@ -43,9 +43,11 @@ import com.mhd.common.core.annotation.fieldsetvalue.NeedSetValueField;
|
||||
import com.mhd.common.security.utils.SecurityUtils;
|
||||
import com.mhd.system.api.model.LoginUser;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import minio.MinIoUploadService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
@@ -95,7 +97,8 @@ public class MenuApplicationService {
|
||||
private SpecialServiceFeign logisticsServiceFeign;
|
||||
@Resource
|
||||
private MenuUtils menuUtils;
|
||||
|
||||
@Autowired
|
||||
private MinIoUploadService minIoUploadService;
|
||||
|
||||
/**
|
||||
* 查询菜单集合
|
||||
@@ -272,6 +275,19 @@ public class MenuApplicationService {
|
||||
return ossService.uploadOss(file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上传文件到oss
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 文件路径
|
||||
*/
|
||||
public String uploadMinio(MultipartFile file)
|
||||
{
|
||||
return minIoUploadService.uploadFileToMinIo(file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据组织id查询菜单集合
|
||||
*
|
||||
|
||||
@@ -162,7 +162,8 @@ public class MenuApi extends BaseController {
|
||||
}
|
||||
|
||||
}
|
||||
return AjaxResult.success(menuApplicationService.uploadOss(fileUpload));
|
||||
//return AjaxResult.success(menuApplicationService.uploadOss(fileUpload));
|
||||
return AjaxResult.success(menuApplicationService.uploadMinio(file));
|
||||
}
|
||||
|
||||
@PostMapping("/uploadFeign")
|
||||
|
||||
@@ -101,6 +101,18 @@
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-sleuth</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>8.4.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.8.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
|
||||
Reference in New Issue
Block a user