日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java文件客户端下载_使用Java写一个minio的客户端上传下载文件

發布時間:2024/1/23 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java文件客户端下载_使用Java写一个minio的客户端上传下载文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

標簽:color???ati???tty???java???system???wired???format???media???param

前言:

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

代碼:

pom.xml

dependency>

groupId>io.miniogroupId>

artifactId>minioartifactId>

version>7.0.2version>

dependency>

application.yml

server:

port:90

minio:

url: http://10.69.94.140:9000

accessKey: 賬號

secretKey: 密碼

defaultFolder:/

MinioProperties.java

@ConfigurationProperties("minio")

@Datapublic classMinioProperties {privateString url;privateString accessKey;privateString secretKey;privateString defaultFolder;

}

SpringConfig.java

@Configuration

@EnableConfigurationProperties(MinioProperties.class)

@Slf4jpublic classSpringConfig {

@AutowiredprivateMinioProperties minioProperties;

@BeanpublicMinioClient minioClient() {try{return newMinioClient(minioProperties.getUrl(), minioProperties.getAccessKey(), minioProperties.getSecretKey());

}catch(Exception e) {

log.error(e.toString());

}return null;

}

}

ImagesController.java

@RestController

@RequestMapping("/image")

@Slf4j

@CrossOrigin(origins= "*")public classImageController {

@AutowiredprivateFileService 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 {returnfileService.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 classFilesController {

@AutowiredprivateFileService 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>

htmllang="en">

head>

metacharset="UTF-8">

title>Upload file testtitle>

head>

body>

formaction="http://localhost:90/image/upload/zeng/test"method="post"enctype="multipart/form-data">

inputtype="file"name="file" />

inputtype="submit"value="Submit">

form>

body>

html>

使用Java寫一個minio的客戶端上傳下載文件

標簽:color???ati???tty???java???system???wired???format???media???param

總結

以上是生活随笔為你收集整理的java文件客户端下载_使用Java写一个minio的客户端上传下载文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。