一、引入依赖
MinIO依赖
<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>
</dependency>
根据扩展名得到mimeType的组件
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.17</version>
</dependency>
二、MultipartFile对象
在SpringMVC中,上传文件我们使用MultipartFile文件接收,我们来看看它提供给我们哪些方法
public interface MultipartFile extends InputStreamSource {
String getName();
@Nullable
String getOriginalFilename();
@Nullable
String getContentType();
boolean isEmpty();
long getSize();
byte[] getBytes() throws IOException;
InputStream getInputStream() throws IOException;
default Resource getResource() {
return new MultipartFileResource(this);
}
void transferTo(File var1) throws IOException, IllegalStateException;
default void transferTo(Path dest) throws IOException, IllegalStateException {
FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest));
}
}
这些方法使我们能够得到上传文件的基本信息:fileName、bytes、contentType以及size
三、常见mimeType
mimeType是描述消息内容类型的标准,用来表示文档、文件或字节流的性质和格式
在文件上传中我们往往需要知道文件的mimeType来选择对应的处理方式
可以使用j256提供的simpleMajic包的ContentInfoUtil类:根据后缀名得到mimeType
ContentInfo contentInfo = ContentInfoUtil.findExtensionMatch(extension);
String contentType = contentInfo.getContentType().getMimeType();
四、minIO文件上传
下载文件
public File downloadFileFromMinIO(File file,String bucket,String objectName){
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucket).object(objectName).build();
try(
InputStream inputStream = minioClient.getObject(getObjectArgs);
FileOutputStream outputStream =new FileOutputStream(file);
) {
IOUtils.copy(inputStream,outputStream);
return file;
}catch (Exception e){
e.printStackTrace();
XueChengPlusException.cast("查询分块文件出错");
}
return null;
}
上传文件
通过bytes上传文件,objectName需要带上文件扩展名,否则该方法会默认contentType为未知二进制数据
private void addMediaFilesToMinIO(byte[] bytes, String bucket, String objectName) {
//资源的媒体类型
String contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;//默认未知二进制流
if (objectName.indexOf(".") >= 0) {
//取objectName中的扩展名
String extension = objectName.substring(objectName.lastIndexOf("."));
//扩展名是否合法
ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
if (extensionMatch != null) {
contentType = extensionMatch.getMimeType();
}
}
try {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucket)
.object(objectName)
//InputStream stream, long objectSize 对象大小, long partSize 分片大小(-1表示5M,最大不要超过5T,最多10000)
.stream(byteArrayInputStream, byteArrayInputStream.available(), -1)
.contentType(contentType)
.build();
//上传到minio
minioClient.putObject(putObjectArgs);
} catch (Exception e) {
e.printStackTrace();
log.debug("上传文件到文件系统出错:{}", e.getMessage());
XueChengPlusException.cast("上传文件到文件系统出错");
}
}
通过文件路径上传文件,当合并文件时我们会创建一个临时文件接收合并的数据,我们需要将该临时文件上传到文件系统中
public void addMediaFilesToMinIO(String filePath, String bucket, String objectName){
try {
UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
.bucket(bucket)
.object(objectName)
.filename(filePath)
.build();
//上传
minioClient.uploadObject(uploadObjectArgs);
log.debug("文件上传成功:{}",filePath);
} catch (Exception e) {
XueChengPlusException.cast("文件上传到文件系统失败");
}
}