java 批量下载文件 打包成zip包

  1. 创建DownLoadUrlStream承载数据
import lombok.Data;
import java.io.InputStream;

@Data
public class DownLoadUrlStream {
    //文件地址 比如/123/234/1.txt
    private String url;
    //文件流
    private InputStream stream;

}
  1. 创建工具类,进行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();
        }
    }
  }
   
  1. 封装数据,调用方法即可
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中实现批量下载并将文件打包ZIP格式,可以使用java.util.zip提供的类和方法。以下是一个简单的实现示例: 首先,需要准备一个文件列表,含要下载文件的路径。可以通过手动指定路径,或者通过程序动态获取,具体根据需求而定。 接下来,需要使用java.util.zip中的ZipOutputStream类和FileInputStream类来实现文件打包。可以创建一个空的ZipOutputStream对象,并使用FileInputStream读取每个文件,然后将其添加到ZipOutputStream中。最后,关闭ZipOutputStream。 同时,还需要使用java.net中的URL和URLConnection类来进行文件下载。可以为每个需要下载文件创建一个URL对象,打开URLConnection连接并获取其输入流。然后,将输入流传递给FileOutputStream,使用java.io中的FileOutputStream类将文件下载到本地。 以下是一个简单的示例代码: import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipDownloader { public static void main(String[] args) { List<String> fileList = getListOfFiles(); // 获取文件列表,具体根据需求实现 String zipFileName = "downloadedFiles.zip"; // 设定打包后的ZIP文件名 try { FileOutputStream fos = new FileOutputStream(zipFileName); ZipOutputStream zos = new ZipOutputStream(fos); for (String file : fileList) { URL url = new URL(file); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); zos.putNextEntry(new ZipEntry(file)); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { zos.write(buffer, 0, length); } is.close(); } zos.closeEntry(); zos.close(); System.out.println("文件下载打包ZIP。"); } catch (IOException e) { e.printStackTrace(); } } } 需要注意的是,该示例只是一个简单的实现,并没有处理异常情况,如文件不存在、网络连接失败等。在实际应用中,需要对这些异常进行适当的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小菜菜程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值