
首先在pom.xml文件中添加MinIO的依赖?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.5.14/version relativePath/ !-- lookup parent from repository -- /parent groupIdcom.example/groupId artifactIdminiosamp/artifactId version0.0.1-SNAPSHOT/version name/ description/ url/ licenses license/ /licenses developers developer/ /developers scm connection/ developerConnection/ tag/ url/ /scm properties java.version17/java.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-thymeleaf/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency dependency groupIdio.minio/groupId artifactIdminio/artifactId version8.5.11/version /dependency /dependencies build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin /plugins /build /project在application.properties文件中配置MinIO的相关参数spring.application.namedemo minio.endpointhttp://192.168.144.134:9000 minio.access-keyminioadmin minio.secret-keyminioadmin minio.bucket-nametest # 设置单个文件大小 spring.servlet.multipart.max-file-size 50MB # 设置单次请求文件的总大小 spring.servlet.multipart.max-request-size 50MB创建一个MinioConfig类 用于加载MinIO的配置信息package com.example.miniosamp; import io.minio.MinioClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class MinioConfig { Value(${minio.endpoint}) private String minioEndpoint; Value(${minio.access-key}) private String minioAccessKey; Value(${minio.secret-key}) private String minioSecretKey; Bean public MinioClient minioClient() { return MinioClient.builder() .endpoint(minioEndpoint) .credentials(minioAccessKey, minioSecretKey) .build(); } }在编写一个controller类即可package com.example.miniosamp; import io.minio.MinioClient; import io.minio.PutObjectArgs; import io.minio.errors.MinioException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; SpringBootApplication RestController public class MinioDemoController { Autowired private MinioClient minioClient; Value(${minio.bucket-name}) private String bucketName; PostMapping(/upload) public String uploadFile(RequestParam(file) MultipartFile file) { try { InputStream inputStream file.getInputStream(); minioClient.putObject( PutObjectArgs.builder() .bucket(bucketName) .object(file.getOriginalFilename()) .stream(inputStream, inputStream.available(), -1) .contentType(file.getContentType()) .build() ); return File uploaded successfully!; } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) { e.printStackTrace(); return Error uploading file to MinIO: e.getMessage(); } } }现在你可以运行这个Spring Boot应用并通过/upload接口上传文件到MinIO。第三步编写前端进行测试前端代码index.html!DOCTYPE html html langzh head meta charsetUTF-8 meta nameviewport contentwidthdevice-width, initial-scale1.0 title文件上传/title /head body h1文件上传/h1 form action/upload methodpost enctypemultipart/form-data input typefile namefile required button typesubmit上传/button /form /body /html