时间:2021-05-19
前言:
确保已经安装了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>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了python实现支持目录FTP上传下载文件的方法。分享给大家供大家参考。具体如下:该程序支持ftp上传下载文件和目录、适用于windows和lin
springboot整合vue实现上传下载文件,供大家参考,具体内容如下环境springboot1.5.x完整代码下载:springboot整合vue实现上传下
本文实例讲述了python实现的简单FTP上传下载文件的方法。分享给大家供大家参考。具体如下:python本身自带一个FTP模块,可以实现上传下载的函数功能。#
文档说明 本文档使用Socket通信方式来实现ftp文件的上传下载等命令的执行1.基本介绍 由于最近的项目是客户端的程序,需要将客户端的图片文件【切图】-【
本文实例讲述了Java实现的文件上传下载工具类。分享给大家供大家参考,具体如下:这是一个在Eclipse环境下采用Java语言实现文件上传下载的工具类。和之前介