springboot整合minio

发布时间:2023-11-30 17:34:51

1.pom.xml文件

<dependency><groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency><!-- minio --><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>3.0.10</version></dependency><!-- alibaba的fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.51</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version></dependency>
2.application.yml文件
minio:  endpoint: http://192.168.31.112:9000  accesskey: minioadmin  secretKey: minioadminspring:  servlet:    multipart:      enabled: true      max-request-size: 100GB      max-file-size: 100GB
3.创建minio配置类别
package com.minio.config;import io.minio.MinioClient;import io.minio.errors.InvalidEndpointException;import io.minio.errors.InvalidPortException;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MinioConfig {        /**     * 连接url     */    @Value("${minio.endpoint}")    private String endpoint;    /**     * 用户名     */    @Value("${minio.accesskey}")    private String accesskey;    /**     * 密码     */    @Value("${minio.secretKey}")    private String secretKey;    @Bean    public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {        return new MinioClient(endpoint , accesskey , secretKey);    }}
4.创建文件处理工具
package com.minio.util;import com.alibaba.fastjson.JSONObject;import io.minio.MinioClient;import io.minio.errors.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile;import org.xmlpull.v1.XmlPullParserException;import java.io.IOException;import java.io.InputStream;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.util.UUID;@Componentpublic class MinioUtils {    @Autowired    private MinioClient minioClient;    /**     * 创建桶     * @param bucketName     */    public void createBucket(String bucketName) throws InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException, NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, InternalException, RegionConflictException {        boolean is = minioClient.bucketExists(bucketName);        if (is){            return;        }        minioClient.makeBucket(bucketName);    }    /**     * 文件上传      * @param file     * @param bucketName     * @return     */    public JSONObject uploadFile(MultipartFile file,String bucketName) {        JSONObject result = new JSONObject();        try {            createBucket(bucketName);            String originalFilename = file.getOriginalFilename(); //文件名            String last = originalFilename.substring(originalFilename.lastIndexOf("."));            String randomID = UUID.randomUUID().toString();            String fileID = randomID + last;            minioClient.putObject(bucketName , fileID , file.getInputStream() , file.getContentType());            result.put("fileName",originalFilename );            result.put("fileID",fileID );        }catch (Exception e){            e.printStackTrace();        }        return result;    }    /**     * 获取文件流     * @param bucketName     * @param fileID     */    public InputStream getInputStream(String bucketName , String fileID) throws InvalidArgumentException, InvalidBucketNameException, InsufficientDataException, XmlPullParserException, ErrorResponseException, NoSuchAlgorithmException, IOException, NoResponseException, InvalidKeyException, InternalException {        return minioClient.getObject(bucketName, fileID);    }}
5.创建上传控制器
package com.minio.controller;import com.alibaba.fastjson.JSONObject;import com.minio.util.MinioUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;@RestController@RequestMapping("file")public class FileController {    private String BUCKET_NAME = "sys-file-bucket";    @Autowired    private MinioUtils minioUtils;    @RequestMapping("uploadFile")    public JSONObject uploadFile(@RequestParam(name = "file") MultipartFile multipartFile){        return minioUtils.uploadFile(multipartFile, BUCKET_NAME);    }}

上一篇 Java 实现计算机程序
下一篇 23种设计模式——单例模式

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题