基于Java写minio客户端实现上传下载文件的方法

这篇文章主要讲解了基于Java写minio客户端实现上传下载文件的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

灵武网站建设公司成都创新互联,灵武网站设计制作,有大型网站制作公司丰富经验。已为灵武上1000家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的灵武做网站的公司定做!

前言:

确保已经安装了minio的服务端

代码:

pom.xml


  io.minio
  minio
  7.0.2

application.yml

server:
 port:90
minio:
 url: http://10.69.94.140:9000
 accessKey: 账号
 secretKey: 密码
 defaultFolder: /

MinioProperties.java

@ConfigurationProperties("minio")
@Data
public class MinioProperties {
  private String url;
  private String accessKey;
  private String secretKey;
  private String defaultFolder;
}

SpringConfig.java

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




  
  Upload file test


  

看完上述内容,是不是对基于Java写minio客户端实现上传下载文件的方法有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


新闻名称:基于Java写minio客户端实现上传下载文件的方法
网址分享:http://hbruida.cn/article/gpppss.html