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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值