Minio配置请点击这里
忽略测试就可以了
图片上传
Controller层
/**
* kc-上传课程封面图片
* Path: /course/upload
* Method: POST
*/
@PostMapping("/upload")
public ResponseResult<String> upload(MultipartFile uploadFile) throws Exception {
return courseBaseService.upload(uploadFile);
}
}
Service层
@Autowired
private MinioClient client;
@Autowired
private MinioProperties properties;
//文件上传
public ResponseResult<String> upload(MultipartFile uploadFile) throws Exception {
//1文件上传流
InputStream inputStream = uploadFile.getInputStream();
//2文件上传路径
String filePath = "/images/" + new SimpleDateFormat("yyyy-MM-dd")
.format(new Date())
+ "/" + UUID.randomUUID().toString() + uploadFile.getOriginalFilename();
//3文件上传对象 PutObjectArgs
PutObjectArgs args = PutObjectArgs.builder()
.object(filePath)
.bucket(properties.getBucket())
.stream(inputStream, inputStream.available(), -1)
.contentType("image.jpg")
.build();
//4通过client上传
client.putObject(args);
//5构造请求地址
String url = properties.getDomain() + "/" + properties.getBucket() + filePath;
//6返回
return ResponseResult.okResult(url);
}