Spring Boot 实现将多个MinIO文件压缩成压缩包并下载至本地

  • Post author:
  • Post category:其他


1 Maven依赖

		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.5.7</version>
		</dependency>
		<dependency>
			<groupId>io.minio</groupId>
			<artifactId>minio</artifactId>
			<version>7.0.2</version>
		</dependency>

2 调试代码

   /**
     * 从MinIO下载多个文件
     *
     * @param response
     */
    @GetMapping("/downloadMultiFileToMinIO")
    public void downloadMultiFileToMinIO(HttpServletResponse response) throws IOException {
        MinioClient minioClient = minIOService.getMinioClient();
        List<String> fileUrlList = new ArrayList<>(Arrays.asList("新建文件夹1.txt","新建文本文档.txt", "myfile_20210515111437.docx"));
        //被压缩文件InputStream
        InputStream[] srcFiles = new InputStream[fileUrlList.size()];
        //被压缩文件名称
        String[] srcFileNames = new String[fileUrlList.size()];
        for (int i = 0; i < fileUrlList.size(); i++) {
            String fileUrl = fileUrlList.get(i);
            InputStream inputStream = minIOService.getObject(minioClient, fileUrl);
            if (inputStream == null) {
                continue;
            }
            srcFiles[i] = inputStream;
            String[] splitFileUrl = fileUrl.split("/");
            srcFileNames[i] = splitFileUrl[splitFileUrl.length - 1];
        }
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("下载测试.zip", "UTF-8"));
        //多个文件压缩成压缩包返回
        ZipUtil.zip(response.getOutputStream(), srcFileNames, srcFiles);
    }

3 MinIO文件

4 调试结果

注:

MinIO的使用及MinIOService源码请查看以下博客。


Spring Boot配置MinIO(实现文件上传、下载、删除)



版权声明:本文为qq_38974638原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。