minio对接
This commit is contained in:
@@ -13,12 +13,13 @@ 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:6001/";
|
||||
private static String host="http://10.102.192.5:6000";
|
||||
private static String accessKey="minioadmin";
|
||||
private static String secretKey="gmminioadmin";
|
||||
/**
|
||||
@@ -34,22 +35,43 @@ public class MinIoUploadService {
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
public boolean uploadFileToMinIo(MultipartFile multipartFile, String fileName) {
|
||||
public String uploadFileToMinIo(MultipartFile multipartFile) {
|
||||
try {
|
||||
initMinio();
|
||||
InputStream inputStream = multipartFile.getInputStream();
|
||||
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
||||
.object(fileName)//文件名
|
||||
.bucket(bucket)//桶名词 与minio创建的名词一致
|
||||
// fileInputStream.available() 表示一直有内容就会上传 -1 表示将所有的相关文件的内容都上传
|
||||
.stream(inputStream, inputStream.available(), -1) //文件流
|
||||
.build();
|
||||
minioClient.putObject(putObjectArgs);
|
||||
return true;
|
||||
|
||||
// 上传文件的名称
|
||||
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 false;
|
||||
return "";
|
||||
}
|
||||
private MinioClient initMinio() {
|
||||
try{
|
||||
@@ -64,6 +86,7 @@ public class MinIoUploadService {
|
||||
return minioClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* 文件上传成功,返回文件完整访问路径
|
||||
|
||||
+8
-3
@@ -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,17 +275,19 @@ public class MenuApplicationService {
|
||||
return ossService.uploadOss(file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上传文件到oss
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 文件路径
|
||||
*/
|
||||
public String uploadMinio(File file)
|
||||
public String uploadMinio(MultipartFile file)
|
||||
{
|
||||
return ossService.uploadOss(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")
|
||||
|
||||
Reference in New Issue
Block a user