POM
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.2.1</version> </dependency>
配置文件
#minio 文件服务器配置 minio: hostUrl: http://localhost:9000 accessKey: admin secretKey: admin bucketName: myfile
Controller/Action:
//minio文件服务器参数 @Value("${minio.hostUrl}") private String minioHostUrl; @Value("${minio.accessKey}") private String minioAccessKey; @Value("${minio.secretKey}") private String minioSecretKey; @Value("${minio.bucketName}") private String minioBucketName; /** * 网页端上传文件(上传到minio文件服务器) */ @RequestMapping(value = "upload", method = RequestMethod.POST) public List<String> upload(HttpServletRequest request) throws Exception { List<String> filePath=new ArrayList<>(); MultipartHttpServletRequest murequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> files = murequest.getFileMap();// 得到文件map对象 List<String> filePath = new ArrayList<String>(); for (MultipartFile file : files.values()) { //源文件名 String originalFilename = file.getOriginalFilename(); //保存文件的路径(如2020/7/10/4d132f66a7b948c882e2c1649a58ab70-file1.jpg),防止文件名重复。 String saveFileName = UUID.randomUUID().toString().replace("-", "").toLowerCase()+"-"+originalFilename; //初始化MinioClient MinioClient minioClient = MinioClient.builder() .endpoint(minioHostUrl) .credentials(minioAccessKey, minioSecretKey) .build(); //上传 minioClient.putObject( PutObjectArgs.builder() .bucket(minioBucketName) //桶名(文件夹名) .stream(file.getInputStream(),file.getSize(), 5*1024*1024) //文件,大小 .object(saveFileName) //文件名 .build()); String backFileName="/"+minioBucketName+"/"+saveFileName; filePath.add(backFileName); } return filePath; }