• java中批量导出文件并压缩到zip文件中


    最近开发遇到个问题 批量导出并压缩到zip文件中,代码和思路如下 不做赘余的解释…

    @PostMapping("/downLoads")
    	public void downLoadCategoryFiles(@RequestBody List<String> fileUids, HttpServletResponse response) throws Exception {
    		// 取得文件信息
    		List<xxx> file = this.service.listByIds(fileUids);
    		if (file.size() == 0) {
    			throw new CheckedException("文件信息丢失");
    		}
    		
    		this.service.getCategoryFiles(fileUids, response);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    service层代码

    void getCategoryFiles(List<String> fileUid, HttpServletResponse response) throws Exception;
    
    • 1

    实现层业务代码如下:

    @Override
    	public void getCategoryFiles(List<String> fileUids, HttpServletResponse response) throws Exception {
    		List<实体类> files = this.listByIds(fileUids);
    		if (files.size() == 0) {
    			throw new RuntimeException("文件已被删除。");
    		}
    		
    		//将路径放到一个list中去
    		List<String> fullPaths = files.stream().map(实体类::getFullPath).collect(Collectors.toList());
    		
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    		String year = sdf.format(new Date());//获取当前年月日
    		
    		//设置文件/名称
    		response.addHeader("Content-Type", "application/zip");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + year + ".zip\"");
    
            //逻辑处理
            try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream()); ) {
                for (String fileName : fullPaths) { //循环往zip中放入文件
                    File file = new File(fileName);
                    FileInputStream fileIn = new FileInputStream(file);
                    ZipEntry zipEntry = new ZipEntry(file.getName());
                    zipOut.putNextEntry(zipEntry);
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = fileIn.read(buffer)) > 0) {
                        zipOut.write(buffer, 0, len);
                    }
                    fileIn.close();
                    zipOut.closeEntry();
                }
                zipOut.finish();
            } catch (Exception e) {
            	throw new RuntimeException("文件批量下载有误.", e);
            }
    	} 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    最后一层 代码中所提到的"实体类",自我填充即可.

  • 相关阅读:
    JavaScript系列从入门到精通系列第十二篇:JavaScript中对象的简介和对象的基本操作以及JavaScript中的属性值和属性名
    Win11怎么修改关机界面颜色?Win11修改关机界面颜色的方法
    javase javaee javame
    OSCAR 分享之蚂蚁开源治理的方法和实践
    滑动窗口总结
    STL中 Map 的基本用法
    fyne - 谁说用Go不能开发应用界面
    【剑指Offer】29.顺时针打印矩阵
    TypeScript 学习笔记
    物理场仿真教程(一)——Ubuntu下Salome_meca 软件安装
  • 原文地址:https://blog.csdn.net/weixin_43967890/article/details/132906658