基于Java写minio客户端实现上传下载文件
前言:
确保已经安装了minio的服务端
代码:
pom.xml
io.minio minio 7.0.2
application.yml
server: port:90 minio: url:http://10.69.94.140:9000 accessKey:账号 secretKey:密码 defaultFolder:/
MinioProperties.java
@ConfigurationProperties("minio") @Data publicclassMinioProperties{ privateStringurl; privateStringaccessKey; privateStringsecretKey; privateStringdefaultFolder; }
SpringConfig.java
@Configuration @EnableConfigurationProperties(MinioProperties.class) @Slf4j publicclassSpringConfig{ @Autowired privateMinioPropertiesminioProperties; @Bean publicMinioClientminioClient(){ try{ returnnewMinioClient(minioProperties.getUrl(),minioProperties.getAccessKey(),minioProperties.getSecretKey()); }catch(Exceptione){ log.error(e.toString()); } returnnull; } }
ImagesController.java
@RestController @RequestMapping("/image") @Slf4j @CrossOrigin(origins="*") publicclassImageController{ @Autowired privateFileServicefileService; /******* *Getimagefile,thismethodreturnanimagetypefilewhichcanbedisplayedinbrowser. *@parambucketName,system,eachsystemshouldbelongaspecialbucket. *@paramcategory,asystemmaycontainmultiplecategory *@paramfileName */ @GetMapping(value="/get/{bucketName}/{category}/{objectName}/{fileName}",produces=MediaType.IMAGE_JPEG_VALUE) publicbyte[]get(@PathVariable("bucketName")StringbucketName,@PathVariable("category")Stringcategory, @PathVariable("objectName")StringobjectName, @PathVariable("fileName")StringfileName)throwsException{ returnfileService.getFile(bucketName,category,objectName); } @GetMapping("/download/{bucketName}/{category}/{objectName}/{fileName}") publicvoiddownload(@PathVariable("bucketName")StringbucketName,@PathVariable("category")Stringcategory, @PathVariable("objectName")StringobjectName, @PathVariable("fileName")StringfileName,HttpServletResponseresponse)throwsException{ byte[]buffer=fileService.getFile(bucketName,category,objectName); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setHeader("Content-disposition","attachment;filename=\""+fileName+"\""); response.getOutputStream().write(buffer); response.flushBuffer(); response.getOutputStream().close(); } @PostMapping("/upload/{bucketName}/{category}") publicStringupload(@PathVariable("bucketName")StringbucketName,@PathVariable("category")Stringcategory, @RequestParam("file")MultipartFilefile)throwsException{ StringobjectName=UUID.randomUUID().toString(); fileService.storeFile(bucketName,category,objectName,file.getBytes()); returnString.format("image/get/%s/%s/%s/%s",bucketName,category,objectName,file.getOriginalFilename()); } }
FilesController.java
@RestController @RequestMapping("/files") @Slf4j @CrossOrigin(origins="*") publicclassFilesController{ @Autowired privateFileServicefileService; @GetMapping("/download/{bucketName}/{category}/{objectName}/{fileName}") publicvoiddownload(@PathVariable("bucketName")StringbucketName,@PathVariable("category")Stringcategory, @PathVariable("objectName")StringobjectName,@PathVariable("fileName")StringfileName,HttpServletResponseresponse)throwsException{ byte[]buffer=fileService.getFile(bucketName,category,objectName); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setHeader("Content-disposition","attachment;filename=\""+fileName+"\""); response.getOutputStream().write(buffer); response.flushBuffer(); response.getOutputStream().close(); } @PostMapping("/upload/{bucketName}/{category}") publicStringupload(@PathVariable("bucketName")StringbucketName,@PathVariable("category")Stringcategory, @RequestParam("file")MultipartFilefile)throwsException{ StringobjectName=UUID.randomUUID().toString(); fileService.storeFile(bucketName,category,objectName,file.getBytes()); returnString.format("files/download/%s/%s/%s/%s",bucketName,category,objectName,file.getOriginalFilename()); } }
upload.html
Uploadfiletest