亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

基于Java寫minio客戶端實現上傳下載文件

瀏覽:4日期:2022-09-01 14:34:21

前言:

確保已經安裝了minio的服務端

代碼:

pom.xml

<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.0.2</version></dependency>

application.yml

server: port:90minio: url: http://10.69.94.140:9000 accessKey: 賬號 secretKey: 密碼 defaultFolder: /

MinioProperties.java

@ConfigurationProperties('minio')@Datapublic class MinioProperties { private String url; private String accessKey; private String secretKey; private String defaultFolder;}

SpringConfig.java

@Configuration@EnableConfigurationProperties(MinioProperties.class)@Slf4jpublic 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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 91欧美国产 | 亚洲一成人毛片 | 极品一区 | 另类重口100页在线播放 | 免费a视频在线观看 | 91久久精品一区二区三区 | 久久精品一区二区三区四区 | 高清性色生活片免费观看 | 日本一道免费一区二区三区 | 免费国产草莓视频在线观看黄 | 国产v国产v片大片线观看网站 | 中文字幕日韩欧美一区二区三区 | 在线观看大片的网站 | 色图在线观看 | 一级做a爱免费观看视频 | 婷婷操| 国产一级理仑片日本 | 蜜桃臀久久伊人福利 | 色香影院 | 97国产成人精品免费视频 | 欧美伦妇高清免费 | 一级a欧美毛片 | 色婷婷六月桃花综合影院 | 久久一区二区三区精品 | 欧美日韩综合精品一区二区三区 | 国产在线观看91精品2021 | 99久热只有精品视频免费观看17 | 在线观看免费视频一区 | 中国美女做爰视频高清 | 久久综合综合 | 故意坐公交忘穿内裤被挺进小说 | jk足控福利国产在线播放 | 亚洲高清视频在线播放 | 国产三级黄色毛片 | 日韩图区 | 99re在线| 日本www色视频成人免费免费 | 97国产在线观看 | 欧美噜噜| 亚洲国产精品久久久久久网站 | 最近手机中文字幕无吗 |