- 集成步骤
集成步骤
- 依赖导入
7.1.0 io.minio minio ${minio.version}
- yml配置
spring:
servlet:
multipart:
#开启文件上传
enabled: true
# 限制文件上传大小为10MB
max-file-size: 10MB
#自定义配置
minio:
url: http://11.11.11.224:9000 #MinIO服务所在地址
bucketName: demo #存储桶名称
accessKey: minioadmin #访问的key/用户名
secretKey: minioadmin #访问的秘钥/密码
- 配置类Minio Bean
@Component
public class Minio {
private final Logger logger = LoggerFactory.getLogger(Minio.class);
//minio地址+端口
@Value("${minio.url}")
private String url;
//minio用户名
@Value("${minio.accessKey}")
private String accessKey;
//minio密码
@Value("${minio.secretKey}")
private String secretKey;
// //文件桶名称
// @Value("${minio.bucketName}")
// private String bucketName;
// //失效日期
// private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;
// @Value("${minio.thumbor.width}")
private String thumborWidth;
public CommonResult minioUpload(MultipartFile file, String fileName, String bucketName) {
try {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
boolean bucketExists = minioClient.bucketExists(bucketName);
if (bucketExists) {
logger.info("仓库" + bucketName + "已经存在,可直接上传文件。");
} else {
minioClient.makeBucket(bucketName);
}
if (file.getSize() <= 20971520) {
// fileName为空,说明要使用源文件名上传
if (fileName == null) {
fileName = file.getOriginalFilename();
fileName = fileName.replaceAll(" ", "_");
}
// minio仓库名
minioClient.putObject(bucketName, fileName, file.getInputStream(), new PutObjectOptions(file.getInputStream().available(), -1));
logger.info("成功上传文件 " + fileName + " 至 " + bucketName);
String fileUrl = bucketName + "/" + fileName;
Map map = new HashMap();
map.put("fileUrl", fileUrl);
map.put("bucketName", bucketName);
map.put("originFileName", fileName);
return CommonResult.success(map);
} else {
throw new Exception("请上传小于20mb的文件");
}
} catch (Exception e) {
e.printStackTrace();
if (e.getMessage().contains("ORA")) {
return CommonResult.failed("上传失败:【查询参数错误】");
}
return CommonResult.failed("上传失败:【" + e.getMessage() + "】");
}
}
public boolean isFileExisted(String fileName, String bucketName) {
InputStream inputStream = null;
try {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
inputStream = minioClient.getObject(bucketName, fileName);
if (inputStream != null) {
return true;
}
} catch (Exception e) {
logger.error(e.getMessage());
return false;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
public boolean delete(String bucketName,String fileName) {
try {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
minioClient.removeObject(bucketName,fileName);
return true;
} catch (Exception e) {
logger.error(e.getMessage());
return false;
}
}
public CommonResult downloadFile(String objectName,String bucketName, HttpServletResponse response) {
try {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
InputStream file = minioClient.getObject(bucketName,objectName);
String filename = new String(objectName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream servletOutputStream = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while((len=file.read(buffer)) > 0){
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
file.close();
servletOutputStream.close();
return CommonResult.success(objectName + "下载成功");
} catch (Exception e) {
e.printStackTrace();
if (e.getMessage().contains("ORA")) {
return CommonResult.success("下载失败:【查询参数错误】");
}
return CommonResult.success("下载失败:【" + e.getMessage() + "】");
}
}
public InputStream getFileInputStream(String objectName,String bucketName) {
try {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
return minioClient.getObject(bucketName,objectName);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
return null;
}
}
- controller层
@Api(tags = "MinioController", description = "MinIO存储管理Controller")
@RequestMapping("/minio")
@RestController
public class MinioController {
private static final Logger logger = LoggerFactory.getLogger(MinioController.class);
//文件桶名称
@Value("${minio.bucketName}")
private String bucketName;
@Autowired
private Minio minio;
@ApiOperation("下载文件Minio")
@RequestMapping("/download
@PostMapping(value = "/upload")
@ApiOperation(value = "图片上传")
public CommonResult upload(HttpServletRequest request, HttpServletResponse response) {
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile mf = multipartRequest.getFile("file");// 获取上传文件对象
String orgName = "";
String fileName = "";
if (mf != null){
orgName = mf.getOriginalFilename();// 获取文件名
fileName = System.currentTimeMillis()+"_"+ orgName.replaceAll(" ", "_");
}
// 步骤一、判断文件是否存在过 存在则不能上传(Minio服务器上传同样位置的同样文件名的文件时,新的文件会把旧的文件覆盖掉)
boolean exist = minio.isFileExisted(fileName, bucketName);
if (exist) {
logger.error("文件 " + fileName + " 已经存在");
return CommonResult.failed("文件已存在");
}
// 步骤二、上传文件
CommonResult result = minio.minioUpload(mf,fileName,bucketName);
return result;
} catch (Exception e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return CommonResult.failed("上传失败");
}
@GetMapping(value = "/view
private static String extractPathFromPattern(final HttpServletRequest request) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
}
}



