• 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

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

  • 相关阅读:
    Redis的分布式锁
    Android studio “Layout Inspector“工具在Android14 userdebug设备无法正常使用
    【react】react-redux 使用指南
    Ftp连接显示connection refused问题的解决
    使用Visual Studio 2022 创建lib和dll并使用
    Google 搜索老矣,尚能饭否?
    【Java基础】面向对象进阶(二)
    cmake应用:集成gtest进行单元测试
    GBase产品学习-GCWare
    同三维T80004EHL-W-4K30 4K HDMI编码器,支持WEBRTC协议
  • 原文地址:https://blog.csdn.net/weixin_43967890/article/details/132906658