springboot +vue 集成minio上传文件

2,104 阅读1分钟

大家好,我是泉城IT,这次跟大家说一下,如何在springboot+vue中使用minio的文件上传,也许很多人用过,这里我简单整理了一下,大家一起交流了

electronics-hi-tech-telasm-wallpaper-tl185.jpg

前台代码

 <el-upload class="upload" action="/api/upload/folder/file" accept=".jpg,.png" :data="{folder:'image'}"
                   :show-file-list="false" :on-progress="uploadProgress" :on-success="uploadSuccess">
          <el-avatar :size="100" :src="form.imagePath" shape="circle" fit="scale-down" class="user-image">暂无头像
          </el-avatar>
        </el-upload>

我们在前台配置好后台的接口地址即可

后台代码

配置minio

打开application.yml

minio:
  endpoint: http://xxxxxxx:9000
  accessKey: AKIAIOSFODNN7EXAMPLE
  secretKey: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
  bucketName: sdd
  

引入pom文件

 <dependency>
           <groupId>io.minio</groupId>
           <artifactId>minio</artifactId>
           <version>3.0.10</version>
       </dependency>

后台代码

@RequestMapping("/folder/file")
    @ResponseBody
    public RestResponse folderFile(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        String fileName = multipartFile.getOriginalFilename();
        long fileSize = multipartFile.getSize();
        String filePath = null;
        String folder = request.getParameter("folder");
        try (InputStream inputStream = multipartFile.getInputStream()) {
            filePath = fileUploadService.fileUpload(inputStream, fileSize, fileName, folder);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return RestResponse.fail(2, "文件上传失败");
        }
        return RestResponse.ok(filePath);
    }
 
 public String fileUpload(InputStream inputStream, long fileSize, String fileName, String folder) {

       return minioUtils.updateFile(inputStream,fileName);
   }
   

引入minio工具类

@Configuration
@Data
@ConfigurationProperties(prefix = "minio")
public class MinioUtils {
    private String endpoint;
    private String accessKey;
    private String secretKey;
    private String bucketName;

    private MinioClient getMinioClient() {
        MinioClient minioClient = null;
        try {
            minioClient = new MinioClient(this.endpoint, this.accessKey, this.secretKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return minioClient;
    }

    public String updateFile(InputStream stream,String fileName) {
        String name = UUID.randomUUID().toString().replaceAll("-", "").concat(getFileType(fileName));
        try {
            getMinioClient().putObject(this.bucketName, name, stream, "application/octet-stream");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this.endpoint.concat("/").concat(this.bucketName).concat("/").concat(name);
    }
    private String getFileType(String fileName){
        int indexOf = fileName.lastIndexOf(".");
        String substring = fileName.substring(indexOf);
        return  substring;
    }
}

完结

这样就完成了springboot 与minio的整合,前端跑起来,上传就可以了