天天看點

MinIO檔案上傳一、引入依賴二、MultipartFile對象三、常見mimeType四、minIO檔案上傳

一、引入依賴

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是描述消息内容類型的标準,用來表示文檔、檔案或位元組流的性質和格式
MinIO檔案上傳一、引入依賴二、MultipartFile對象三、常見mimeType四、minIO檔案上傳

在檔案上傳中我們往往需要知道檔案的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("檔案上傳到檔案系統失敗");
        }
    }