要实现一个资源库的功能,实现图片、文件、视频等资源的上传和下载。
项目git地址,还没完全完成
https://github.com/zhumengting/resource-management
MinIO 是在 Apache License v2.0 下发布的对象存储服务器,免费、轻量、部署简单。
windows 64位
https://dl.min.io/server/minio/release/windows-amd64/minio.exe
minio.exe server E:\share
Linux 64位
https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
./minio server /data
pom依赖
<!--minio-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>6.0.8</version>
</dependency>
config配置,在对应的yml文件需要配置相关属性
@Configuration
public class MinioConfig {
@Value("${minio.client}")
public String minioUrl;
@Value("${minio.accessKey}")
public String minioAccessKey;
@Value("${minio.secretKey}")
public String minioSecretKey;
@Bean
public MinioClient getMinioClient() throws InvalidPortException, InvalidEndpointException {
MinioClient minioClient = new MinioClient(minioUrl,minioAccessKey,minioSecretKey);
return minioClient;
}
}
服务层
@Service
public class MinioCloudStorageService implements CloudStorageService {
private static final Logger logger = LoggerFactory.getLogger(MinioCloudStorageService.class);
@Autowired
private MinioClient minioClient;
@Override
public String upload(String bucketName, InputStream inputStream, String fileName, String mediaType) {
try {
minioClient.putObject(bucketName, fileName, inputStream, mediaType);
} catch (Exception e) {
logger.info("minio上传文件失败,请检查配置信息");
throw new CommonException("minio上传文件失败,请检查配置信息");
}
return bucketName + "/" + fileName;
}
@Override
public String uploadSuffix(InputStream inputStream, String suffix) {
String fileName = MinioTypeUtil.getFileName(suffix);
String mediaType = MinioTypeUtil.getMediaType(suffix);
String bucketName = MinioTypeUtil.getBucketName(suffix);
return upload(bucketName, inputStream, fileName, mediaType);
}
@Override
public String uploadSuffix(byte[] data, String suffix) {
return uploadSuffix(new ByteArrayInputStream(data), suffix);
}
@Override
public String getPath(String bucketName, String fileName) {
String url;
try {
url = minioClient.getObjectUrl(bucketName, fileName);
} catch (Exception e) {
logger.info("minio获取资源url失败");
throw new CommonException("minio获取资源url失败");
}
return url;
}
@Override
public boolean deleteFile(String bucketName, String fileName) {
try {
minioClient.removeObject(bucketName, fileName);
} catch (Exception e) {
logger.info("minio删除文件失败");
return false;
}
return true;
}
}
util
public class MinioTypeUtil {
private static final Logger logger = LoggerFactory.getLogger(MinioTypeUtil.class);
public static String getMediaType(String suffix) throws CommonException {
switch (suffix) {
case ".png":
return MediaType.IMAGE_PNG_VALUE;
case ".jpeg":
return MediaType.IMAGE_JPEG_VALUE;
case ".docx":
case ".doc":
return MediaType.TEXT_PLAIN_VALUE;
case ".pdf":
return MediaType.APPLICATION_PDF_VALUE;
case ".ppt":
case ".pptx":
case ".avi":
case ".mp4":
case ".mp3":
return MediaType.MULTIPART_MIXED_VALUE;
}
logger.info("您上传资源类型暂不支持");
throw new CommonException("您上传资源类型暂不支持");
}
public static String getBucketName(String suffix) {
switch (suffix) {
case ".png":
case ".jpeg":
return "photo";
case ".docx":
case ".doc":
case ".pdf":
return "word";
case ".ppt":
case ".pptx":
return "ppt";
case ".avi":
case ".mp4":
return "video";
case ".mp3":
return "audio";
}
logger.info("您上传资源类型暂不支持");
throw new CommonException("您上传资源类型暂不支持");
}
/**
* 获取文件名
*
* @param suffix 后缀
* @return 返回上传文件名
*/
public static String getFileName(String suffix) {
//生成uuid
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
return uuid + suffix;
}
}
Controller层
/**
* 上传文件
*
* @param file 文件
* @return 返回值
*/
@PostMapping("/upload")
public Result upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
Result.failure(ResultCode.PARAM_IS_BLANK);
}
String url = null;
try {
//上传文件
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
url = minioCloudStorageService.uploadSuffix(file.getBytes(), suffix);
} catch (Exception e) {
return Result.failure(ResultCode.FILE_UPLOAD_FAIL);
}
return Result.success();
}
spring:
servlet:
multipart:
max-file-size: 200MB
max-request-size: 200MB