国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

基于Java寫minio客戶端實(shí)現(xiàn)上傳下載文件

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

前言:

確保已經(jīng)安裝了minio的服務(wù)端

代碼:

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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 久久国产精品影院 | 久久91这里精品国产2020 | 日韩国产欧美一区二区三区在线 | 国产美女做爰免费视频软件 | 欧美精品成人久久网站 | 欧美日韩国产亚洲综合不卡 | 欧美大屁股精品毛片视频 | 亚洲国内精品 | 亚洲线精品久久一区二区三区 | 国产盗摄一区二区 | 欧美一级毛片无遮无挡 | 爽爽日本在线视频免费 | 日韩欧美中文字幕在线播放 | 欧美视频在线观 | 国产a级一级久久毛片 | 国产99久久九九精品免费 | 经典国产乱子伦精品视频 | 日本特黄特色免费大片 | 69中国xxxxxxxx18| 亚洲欧美视频在线观看 | 2022国产精品手机在线观看 | 中国胖女人一级毛片aaaaa | 国内国语一级毛片在线视频 | 99国产精品久久久久久久... | 日韩中文字幕免费观看 | 国产成人精品视频午夜 | 999成人国产精品 | 成人国产网站 | 日韩精品亚洲一级在线观看 | 亚洲精品手机在线 | 欧美日韩精品一区二区三区视频 | 欧美高清一级毛片免费视 | 国产在线一区二区 | 久久色精品 | 日韩黄在线观看免费视频 | 在线免费公开视频 | 欧美高清视频在线 | 欧美一级乱理片免费观看 | 免费国产成人 | 一级做a爰片久久毛片苍井优 | 欧美久久精品 |