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);
            }
    }

# 样例

# # 单文件

# # 多文件


相关推荐
血小板要健康10 小时前
Java基础常见面试题复习合集1
java·开发语言·经验分享·笔记·面试·学习方法
淼淼76310 小时前
安装jdk1.8
java·开发语言
毕设源码-朱学姐11 小时前
【开题答辩全过程】以 高校食堂餐饮管理系统的设计与实现为例,包含答辩的问题和答案
java
过期动态11 小时前
Java开发中的@EnableWebMvc注解和WebMvcConfigurer接口
java·开发语言·spring boot·spring·tomcat·maven·idea
摇滚侠11 小时前
IDEA 定义返回值快捷键
java·ide·intellij-idea
毕设源码-郭学长11 小时前
【开题答辩全过程】以 高校考勤管理系统为例,包含答辩的问题和答案
java·eclipse
A懿轩A11 小时前
【Maven 构建工具】从零到上手 Maven:安装配置 + IDEA 集成 + 第一个项目(保姆级教程)
java·maven·intellij-idea
野犬寒鸦12 小时前
从零起步学习并发编程 || 第一章:初步认识进程与线程
java·服务器·后端·学习
我爱娃哈哈12 小时前
SpringBoot + Flowable + 自定义节点:可视化工作流引擎,支持请假、报销、审批全场景
java·spring boot·后端
XiaoFan01212 小时前
将有向工作流图转为结构树的实现
java·数据结构·决策树