SpringBoot文件下载(多文件以zip形式,单文件格式不变)

SpringBoot文件下载(多文件以zip形式,单文件格式不变)

初始化文件服务器(我的是minio)

java 复制代码
    private static MinioClient minioClient = null;
    private static String BUCKETNAME;
    public void init() {
        String endpoint = "endpoint";
        int port = 9000;
        String accessKey = "accessKey";
        String secretKey = "secretKey";
        boolean secure = false;

        try {
            minioClient = new MinioClient(endpoint, port, accessKey, secretKey, secure);
            if (!minioClient.bucketExists(BUCKETNAME)) {
                minioClient.makeBucket(BUCKETNAME);
                String config = "{\"Statement\":[{\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Effect\":\"Allow\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::" + BUCKETNAME + "\"},{\"Action\":\"s3:GetObject\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::" + BUCKETNAME + "/*\"}],\"Version\":\"2012-10-17\"}";
                minioClient.setBucketPolicy(BUCKETNAME, config);
            }
        } catch (Exception var7) {
            throw new RuntimeException(var7);
        }
    }

文件下载

java 复制代码
	// fileName : /0/0/2025-9/合格率明细(2025-09).xlsx
    public void downInfo(HttpServletResponse response, String ... fileName) {
	
        if(fileName.length == 1){
        	// 单文件下载
            downLoadOne(response,fileName[0]);
        }else{
        	//多文件下载
            downZip(response,fileName);
        }
    }
    void downLoadOne(HttpServletResponse response,  String filePath){
        try {
            InputStream fileStream = minioClient.getObject(BUCKETNAME, filePath);
            String baseName = FilenameUtils.getBaseName(filePath);
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(baseName, "UTF-8"));
            //文件流输出
            OutputStream out = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fileStream.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            out.flush();
            out.close();
            fileStream.close();
        } catch (Exception err) {
            throw new RuntimeException("文件下载失败", err);
        }
    }

    public void downZip(HttpServletResponse response,String ... fileName) {

        FileServer fileServer = FileServerFactory.get(null);
        // 设置响应头
        response.setContentType("application/zip");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"download.zip\"");
        // 创建流式响应
            try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
                for (String filePath : fileName) {
                    // 获取文件流
                    InputStream fileStream = minioClient.getObject(BUCKETNAME, filePath);
                    // 创建ZIP条目
                    String baseName = FilenameUtils.getBaseName(filePath);
                    ZipEntry zipEntry = new ZipEntry(baseName);
                    zipOutputStream.putNextEntry(zipEntry);
                    // 写入数据
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = fileStream.read(buffer)) != -1) {
                        zipOutputStream.write(buffer, 0, bytesRead);
                    }
                    // 关闭条目和流
                    zipOutputStream.closeEntry();
                    fileStream.close();
                }
                zipOutputStream.finish();

            } catch (Exception e) {
                throw new RuntimeException("文件下载失败", e);
            }
    }

# 样例

# # 单文件

# # 多文件


相关推荐
不知名的老吴8 分钟前
线程的生命周期之线程同步
java·开发语言·jvm
协享科技11 分钟前
Spring Boot 与 Go 双服务架构实践:从单体拆分到通信设计
java·人工智能·spring boot·后端·架构·golang·ai编程
柒和远方18 分钟前
后端认证、鉴权、高并发:从 Session 到 JWT 再到 Redis
前端·后端·面试
dearxue41 分钟前
这一次,我们一起把AI的复杂一口吃掉
人工智能·后端
打字机v1 小时前
OOP 面向对象 java 基础--服务+maven+mysql
后端
fliter1 小时前
Rust 项目管理动态 — 2026 年 2 月
后端
码语智行1 小时前
地图上图、空间拓扑查询示例
java·arcgis
苍何1 小时前
一个令人惊艳的开源项目,Agent Skill 开始自进化了?
后端
程序员黑豆1 小时前
AI全栈开发 - Java:变量
java·前端·ai编程
我是一颗柠檬1 小时前
【Java项目技术亮点】分库分表+数据路由策略:单表5000万后的架构升级方案
java·开发语言·分布式·架构