java批量下载生成zip压缩包的思路详解
作者:夏夜里的晚风。
这篇文章主要介绍了java批量下载生成zip压缩包的思路详解,设计思路大概是本地先创建一个zip文件,将批量下载的文件依次放入zip文件中,将zip文件返回给前端,本文结合实例代码讲解的非常详细,需要的朋友可以参考下
设计思路:
1.本地先创建一个zip文件
2.将批量下载的文件依次放入zip文件中
3.将zip文件返回给前端
//一、本地先生成zip文件 //要批量下载的文件id数组 String[] ids = new String[] {"1","2"} byte[] buffer = new byte[1024]; //创建zip String localZipFile = "D:/temp/test.zip" ; if(!new File(localZipFile).exists()){ new File(localZipFile).mkdirs(); } ZipOutputStream out = new ZipOutputStream(new FileOutputStream(localZipFile)); //依次获取批量下载的文件 for(int i =0; i<ids.length;i++){ //设置压缩包内的文件的字符编码,不然文件名可能变成乱码(用户为windows系统) out.setEncoding("GBK"); //从数据库中获取文件的路径和文件名,并放入zip文件中 String fileId = ids[i]; Map<String,Object> map =exportManagerService.getFjInfo(fileId); FileInputStream inStream = new FileInputStream(new File(map.get("path").toString())); out.putNextEntry(new ZipEntry(map.get("name").toString())); int len; //读入需要下载的文件的内容,打包到zip文件 while ((len = inStream.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeEntry(); inStream.close(); } out.close(); this.downFile(response,"D:/temp","test.zip"); //二、将本地zip返回给前端 private void downFile(HttpServletResponse response,String FilePath, String str) { Map m = new HashMap(); try { String path = FilePath +"/"+ str; File file = new File(path); if (file.exists()) { InputStream ins = new FileInputStream(path); BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面 OutputStream outs = response.getOutputStream();// 获取文件输出IO流 BufferedOutputStream bouts = new BufferedOutputStream(outs); response.addHeader("content-disposition", "attachment;filename=" + java.net.URLEncoder.encode(str, "UTF-8")); int bytesRead = 0; byte[] buffer = new byte[8192]; // 开始向网络传输文件流 while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) { bouts.write(buffer, 0, bytesRead); } bouts.flush();// 这里一定要调用flush()方法 ins.close(); bins.close(); outs.close(); bouts.close(); } } catch (IOException e) { m.put("code", "-1"); m.put("text", "附件下载出错:" + e.getMessage()); e.printStackTrace(); } }
补充:
java 批量下载文件 打包成zip包
创建DownLoadUrlStream承载数据
import lombok.Data; import java.io.InputStream; @Data public class DownLoadUrlStream { //文件地址 比如/123/234/1.txt private String url; //文件流 private InputStream stream; }
创建工具类,进行zip包压缩
import com.valid.util.view.DownLoadUrlStream; import com.valid.util.view.DownLoadUrlString; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @Component public class DownloadUtils { /** * * @param downLoadUrlStreams 文件信息 地址 流 * @param zipName 压缩包名称 * @param response * @throws IOException */ public void downloadFolder(List<DownLoadUrlStream> downLoadUrlStreams,String zipName, HttpServletResponse response) throws IOException { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(zipName,"UTF-8") + "\""); Set<String> paths = new HashSet<>(); // 用于记录已经添加到压缩文件中的目录 try { ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream()); for (DownLoadUrlStream file : downLoadUrlStreams) { String virtualPath = file.getUrl().substring(1); String[] pathArray = virtualPath.split("/"); //构建文件的目录结构 StringBuilder sb = new StringBuilder(); for (int i = 0; i < pathArray.length - 1; i++) { sb.append(pathArray[i]).append("/"); String folderPath = sb.toString(); if (!paths.contains(folderPath)) { // 如果该目录还未被添加到压缩文件中,则添加目录 ZipEntry folderEntry = new ZipEntry(folderPath); zipOut.putNextEntry(folderEntry); zipOut.closeEntry(); paths.add(folderPath); // 将新添加的目录记录到集合中 } } ZipEntry entry = new ZipEntry(virtualPath); zipOut.putNextEntry(entry); //将文件流写入文件中 InputStream inputStream = file.getStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) > 0) { zipOut.write(buffer, 0, len); } inputStream.close(); zipOut.closeEntry(); } zipOut.flush(); zipOut.close(); } catch (IOException e) { e.printStackTrace(); } } }
封装数据,调用方法即可
到此这篇关于java批量下载生成zip压缩包的思路详解的文章就介绍到这了,更多相关java批量下载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!