• SpringBoot 文件批量压缩下载


    工具类ZipUtil

    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 qhong
     * @date 2020/4/23 17:20
     **/
    @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<String> 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 (String url : urlList) {
    			try {
    				URL link = new URL(url);
    				String filename = FileUtil.getLinkName(url);
    				//添加ZipEntry,并ZipEntry中写入文件流
    				//这里,加上i是防止要下载的文件有重名的导致下载失败
    				zipos.putNextEntry(new ZipEntry(filename));
    				os = new DataOutputStream(zipos);
    				InputStream is =  new BufferedInputStream(link.openStream());
    				byte[] b = new byte[100];
    				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();
    		}
    	}
    }
    

    调用

    	/**
    	 * 批量下载操作
    	 * @param request
    	 * @return
    	 */
    	@Override
    	public DataResponse zipDownload(@RequestBody @Validated ZipDownloadRequest request){
    		if (CollectionUtils.isEmpty(request.getUrlList())) {
    			throw new HuishiWebException("批量下载的url地址列表不能为空");
    		}
    		ZipUtil.downZip(request.getUrlList(), request.getDownloadName());
    		return DataResponse.success();
    	}
    
  • 相关阅读:
    iPad开发学习之一环境搭建和基本概念
    介绍一款网页布局设计的工具Balsamiq Mockups
    JS怎么判断是哪个元素触发了点击事件?
    古希腊神话剧本
    WPS知识点
    PPT动画中点击、之前、之后的区别
    JS给网页中的选项添加标点
    调音师、调灯师
    输入密码后,可以修改指定列,其余信息不允许修改
    HiveQL详解
  • 原文地址:https://www.cnblogs.com/hongdada/p/12765741.html
Copyright © 2020-2023  润新知