Hello everyone, I’m Quan City IT, this time to tell you how to use minio file upload in SpringBoot + Vue, maybe a lot of people have used IT, here I briefly organized a little, we communicate together

The front desk code

<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>Copy the code

We can configure the interface address of the background in the foreground

The background code

Configuration minio

Open the application. Yml

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

Import poM files

<dependency> <groupId> IO. Minio </groupId> <artifactId>minio</artifactId> <version>3.0.10</version>Copy the code

The background code

@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, "File upload failed "); } return RestResponse.ok(filePath); }Copy the code
 public String fileUpload(InputStream inputStream, long fileSize, String fileName, String folder) {

       return minioUtils.updateFile(inputStream,fileName);
   }
   
Copy the code

Introduce the Minio utility class

@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; }}Copy the code

The end

This completes springBoot’s integration with Minio, with the front end running and uploading ready