SpringBoot实现压缩包批量文件下载

1.ZipUtil工具类

1.1请求对象

import lombok.AllArgsConstructor
import lombok.NoArgsConstructor
import lombok.Data

/**
 * @author kiki
 * @date 2021/08/27
 **/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class FileRequest{
	private String realUrl;
	private String fileName;
}
import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author kiki
 * @date 2021/08/27
 **/
@Component
public class ZipUtil {

	private static HttpServletResponse response;

	@Autowired
	private HttpServletResponse response2;

	private static HttpServletRequest request;

	@Autowired
	private HttpServletRequest request2;

	@PostConstruct
	public void beforeInit() {
		request=request2;
		response=response2;
	}

	/**
	 * 批量文件压缩下载
	 * @param urlList 需要批量下载文件的链接地址列表
	 * @param zipName 输出的压缩包名称
	 */
	public static void downZip(List<FileRequest> urlList,String zipName){
		//响应头的设置
		response.reset();
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		String downloadName = zipName+".zip";
		//返回客户端浏览器的版本号、类型
		String agent = request.getHeader("USER-AGENT");
		try {
			//针对IE或者以IE为内核的浏览器:
			if (agent.contains("MSIE")||agent.contains("Trident")) {
				downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
			} else {
				//非IE浏览器的处理:
				downloadName = new String(downloadName.getBytes("UTF-8"),"ISO-8859-1");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");

		//设置压缩流:直接写入response,实现边压缩边下载
		ZipOutputStream zipos = null;
		try {
			zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
			zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
		} catch (Exception e) {
			e.printStackTrace();
		}
		//循环将文件写入压缩流
		DataOutputStream os = null;

		for (FileRequest request : urlList) {
			try {
				File file = new File(request.getRealUrl);
				//此处应该加个文件是否存在的判断
				String filename = request.getFileName();
				//添加ZipEntry,并ZipEntry中写入文件流
				zipos.putNextEntry(new ZipEntry(filename));
				os = new DataOutputStream(zipos);
				FileInputStream is =  new FileInputStream(file);
				byte[] b = new byte[1024];
				int length = 0;
				while((length = is.read(b))!= -1){
					os.write(b, 0, length);
				}
				is.close();
				zipos.closeEntry();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//关闭流
		try {
			os.flush();
			os.close();
			zipos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

2.Service层实现代码如下

这里controller层及service接口就不贴出了,比较简单

	@Override
	public CommonResult zipDownload(@RequestBody List<FileRequest> requestList){
		//由于是演示,此处只做简单异常判断,如果想要更完善,可以自定义系统异常[本人gitee有相关配置自定义全局系统异常的示例项目]
		if(requestList.size <= 0){
			return new CommonResult(4444,"文件路径为空");
		}
		String zipName = new SimDateFormat("yyyyMMdd").format(new Date());
		ZipUtil.downZip(requestList, request.getDownloadName());
		return new CommonResult(1111,"压缩包下载完成");
	}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

慕木兮人可

感谢支持,勿忘初心

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

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

打赏作者

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

抵扣说明:

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

余额充值