基于Java寫(xiě)minio客戶(hù)端實(shí)現(xiàn)上傳下載文件
前言:
確保已經(jīng)安裝了minio的服務(wù)端
代碼:
pom.xml
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.0.2</version> </dependency>
application.yml
server: port:90 minio: url: http://10.69.94.140:9000 accessKey: 賬號(hào) secretKey: 密碼 defaultFolder: /
MinioProperties.java
@ConfigurationProperties("minio") @Data public class MinioProperties { private String url; private String accessKey; private String secretKey; private String defaultFolder; }
SpringConfig.java
@Configuration @EnableConfigurationProperties(MinioProperties.class) @Slf4j public class SpringConfig { @Autowired private MinioProperties minioProperties; @Bean public MinioClient minioClient() { try { return new MinioClient(minioProperties.getUrl(), minioProperties.getAccessKey(), minioProperties.getSecretKey()); } catch (Exception e) { log.error(e.toString()); } return null; } }
ImagesController.java
@RestController @RequestMapping("/image") @Slf4j @CrossOrigin(origins = "*") public class ImageController { @Autowired private FileService fileService; /******* * Get image file, this method return an image type file which can be displayed in browser. * @param bucketName, system, each system should belong a special bucket. * @param category, a system may contain multiple category * @param fileName */ @GetMapping(value = "/get/{bucketName}/{category}/{objectName}/{fileName}", produces = MediaType.IMAGE_JPEG_VALUE) public byte[] get(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category, @PathVariable("objectName") String objectName, @PathVariable("fileName") String fileName) throws Exception { return fileService.getFile(bucketName, category, objectName); } @GetMapping("/download/{bucketName}/{category}/{objectName}/{fileName}") public void download(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category, @PathVariable("objectName") String objectName, @PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception { 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}") public String upload(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category, @RequestParam("file") MultipartFile file) throws Exception { String objectName = UUID.randomUUID().toString(); fileService.storeFile(bucketName, category, objectName, file.getBytes()); return String.format("image/get/%s/%s/%s/%s", bucketName, category, objectName, file.getOriginalFilename()); } }
FilesController.java
@RestController @RequestMapping("/files") @Slf4j @CrossOrigin(origins = "*") public class FilesController { @Autowired private FileService fileService; @GetMapping("/download/{bucketName}/{category}/{objectName}/{fileName}") public void download(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category, @PathVariable("objectName") String objectName, @PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception { 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}") public String upload(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category, @RequestParam("file") MultipartFile file) throws Exception { String objectName = UUID.randomUUID().toString(); fileService.storeFile(bucketName, category, objectName, file.getBytes()); return String.format("files/download/%s/%s/%s/%s", bucketName, category, objectName, file.getOriginalFilename()); } }
upload.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload file test</title> </head> <body> <form action="http://localhost:90/image/upload/zeng/test" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="Submit"> </form> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ubuntu 使用Jni開(kāi)發(fā)實(shí)例詳解
這篇文章主要介紹了Ubuntu 使用Jni開(kāi)發(fā)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10基于SpringBoot+Redis的Session共享與單點(diǎn)登錄詳解
這篇文章主要介紹了基于SpringBoot+Redis的Session共享與單點(diǎn)登錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07java并發(fā)容器CopyOnWriteArrayList實(shí)現(xiàn)原理及源碼分析
這篇文章主要為大家詳細(xì)介紹了java并發(fā)容器CopyOnWriteArrayList實(shí)現(xiàn)原理及源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05springboot+redis+阿里云短信實(shí)現(xiàn)手機(jī)號(hào)登錄功能
這篇文章主要介紹了springboot+redis+阿里云短信實(shí)現(xiàn)手機(jī)號(hào)登錄功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01Mybatis Log控制臺(tái)如何輸出打印SQL語(yǔ)句
這篇文章主要介紹了Mybatis Log控制臺(tái)如何輸出打印SQL語(yǔ)句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07mybatis通過(guò)中間表實(shí)現(xiàn)一對(duì)多查詢(xún)功能
這篇文章主要介紹了mybatis通過(guò)中間表實(shí)現(xiàn)一對(duì)多查詢(xún),通過(guò)一個(gè)學(xué)生的id查詢(xún)出該學(xué)生所學(xué)的所有科目,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧分享
Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12