@RequestMapping("/toDowmNoteBygid")public void toDowmNoteBygid(List<Notes> urls , HttpServletRequest request,HttpServletResponse response, PrintWriter pw) {String gname="下载保存文件名"// 响应头的设置response.reset();response.setCharacterEncoding("utf-8");    response.setContentType("multipart/form-data");               // 设置压缩包的名字// 解决不同浏览器压缩包名字含有中文时乱码的问题String downloadName = gname+".zip";String agent = request.getHeader("USER-AGENT");try {if (agent.contains("MSIE") || agent.contains("Trident")) {downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");} else {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;int t =0;for (Notes notes : urls) {t=t+1;//String fileName = getFileTypeByUrl(notes.getNotesUrl());//通过连接获取文件类型 文件名没有有文件类型后缀的时候使用String fileName=t+notes.getNotesName();    File file = UrlFilesToZip.getFileByUrl(notes.getNotesUrl(), fileName);try {// 添加ZipEntry,并ZipEntry中写入文件流// 这里,加上t是防止要下载的文件有重名的导致下载失败zipos.putNextEntry(new ZipEntry(fileName));os = new DataOutputStream(zipos);InputStream is = new FileInputStream(file);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();}}}

引用的工具类

package com.csdn.utils;import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;public class UrlFilesToZip {private static final Logger logger = LoggerFactory.getLogger(UrlFilesToZip.class);//url转filepublic static File getFileByUrl(String fileUrl, String suffix) {ByteArrayOutputStream outStream = new ByteArrayOutputStream();BufferedOutputStream stream = null;InputStream inputStream = null;File file = null;try {URL imageUrl = new URL(fileUrl);HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");inputStream = conn.getInputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inputStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}file = File.createTempFile("file", suffix);FileOutputStream fileOutputStream = new FileOutputStream(file);stream = new BufferedOutputStream(fileOutputStream);stream.write(outStream.toByteArray());} catch (Exception e) {} finally {try {if (inputStream != null)inputStream.close();if (stream != null)stream.close();outStream.close();} catch (Exception e) {}}return file;}//根据获取文件后缀private String getFileTypeByUrl(String url) {String suffixes = "3fr|arw|bmp|cr2|crw|dcr|dng|eps|erf|gif|icns|ico|jpeg|jpg|mos|mrw|nef|odd|orf|pdf|pef|png|ppm|ps|psd|raf|raw|svg|svgz|tif|tiff|webp|x3f|xcf|xps|     7z|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|dmg|eml|gz|img|iso|jar|lha|lz|lzma|lzo|rar|rpm|rz|tar|tar.7z|tar.bz|tar.bz2|tar.gz|tar.lzo|tar.xz|tar.Z|tbz|tbz2|tgz|tZ|tzo|xz|z|zip|aac|ac3|aif|aifc|aiff|amr|caf|flac|m4a|m4b|mp3|oga|ogg|sf2|sfark|voc|wav|weba|wma|      3g2|3gp|3gpp|avi|cavs|dv|dvr|flv|gif|m2ts|m4v|mkv|mod|mov|mp4|mpeg|mpg|mts|mxf|ogg|rm|rmvb|swf|ts|vob|webm|wmv|wtv|     abw|djvu|doc|docm|docx|html|lwp|md|odt|pages|pages.zip|pdf|rst|rtf|sdw|tex|txt|wpd|wps|zabw|eps|html|key|key.zip|odp|pdf|pps|ppsx|ppt|pptm|pptx|ps|sda|swf|     csv|html|numbers|numbers.zip|ods|pdf|sdc|xls|xlsm|xlsx|azw|azw3|azw4|cbc|cbr|cbz|chm|docx|epub|fb2|htm|html|htmlz|lit|lrf|mobi|odt|oeb|pdb|pdf|pml|prc|rb|rtf|snb|tcr|txt|txtz|eot|otf|ttf|woff|dwg|dxf|ai|cdr|cgm|emf|eps|pdf|ps|sk|sk1|svg|svgz|vsd|wmf|website";Pattern pat = Pattern.compile("[\\w]+[\\.](" + suffixes + ")");// 正则判断Matcher mc = pat.matcher(url);// 条件匹配String substring = "";while (mc.find()) {substring = mc.group();// 截取文件名后缀名}return substring;}
}

根据多个网络url打压缩包批量下载文件---java相关推荐

  1. python 根据 url 批量下载文件到本地

    需求说明 将服务器上的文件(动物图片)批量下载到本地,并保留服务器上的目录结构. 一个很好的下载方法是:将服务器上的文件压缩为一个压缩包,然后直接下载一个压缩包到本地即可. 该方案缺点:压缩后的压缩包 ...

  2. 批量下载文件,打包成zip压缩包

    批量下载文件,用程序打成zip压缩包在下载 前台传来要下载的url数组 @RequestMapping(value = "/download",method = RequestMe ...

  3. php批量下载TXT中的URL,需求是这样,再不改变目录结构的前提下,通过URL,批量下载文件,我已经写好了一点儿了...

    //需求是这样,再不改变目录结构的前提下,通过URL,批量下载文件,我已经写好了一点儿了 //但是有点bug,只能下载最后一个文件 //邱老师解决一下 $file=fopen("q.txt& ...

  4. 已知url格式,用wget批量下载文件

    已知文件url格式 https://xxxxx/1.png https://xxxxx/2.png - 通过python使用wget实现批量下载文件: 1.安装wget pip install wge ...

  5. JAVA 批量下载文件

    最近项目有个需求,用户想对挂有附件的数据记录 实现一键下载全部附件(目前项目仅支持每次点击单条记录进行附件下载),下面记录我实现的解决方案. 项目框架基于SSM service业务实现层(impl): ...

  6. 批量下载文件的设备和方法

    申请日期:2014年11月20日 申请人:北京安奇智联科技有限公司 [摘要] 本发明公开了一种驻留在移动终端中的批量下载文件的设备和方法.移动终端能够通过服务器与计算设备的浏览器相连.该批量下载文件的 ...

  7. python实战!智能翻页批量下载文件

    python爬虫遇到爬取文件内容时,需要一页页的翻页爬取,这样很是麻烦,其实可以获取每个列表信息下的文件名和文件链接,让文件名和文件链接处理为列表,保存后下载,实现智能翻页批量下载文件,本文以以京客隆 ...

  8. java批量下载文件为zip包

    批量下载文件为zip包的工具类 package com.meeno.trainsys.util;import javax.servlet.http.HttpServletRequest; import ...

  9. python批量下载文件-Python实现批量下载文件

    Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_a ...

最新文章

  1. 用Windows电脑训练深度学习模型?超详细配置教程来了
  2. mysql邮箱认证_邮箱验证功能的实现
  3. 第23件事 评估产品或项目是否靠谱的7个标准
  4. 编写代码的「八荣八耻」- 以用户易用为荣,以复杂歧义为耻
  5. [翻译]Triggerless design.md
  6. java实现账号单一ip登录,使用Java实现简单后台访问并获取IP示例
  7. 树上倍增求LCA及例题
  8. 网信办:2021年全国受理网络违法和不良信息举报1.66亿件
  9. IDEA部署项目和多余的项目删掉的演示
  10. Faster R-CNN源码中RPN的解析(自用)
  11. 2015年9大优秀项目管理工具集锦
  12. 基于模型与不基于模型的深度增强学习_CVPR2018: 基于时空模型无监督迁移学习的行人重识别...
  13. Linux OpenSSH后门的添加与防范
  14. [转载]Badboy使用教程
  15. 《流程的永恒之道:工作流及BPM技术的理论、规范、模式及最佳实践》书籍导读
  16. 实时渲染技术和DLSS 2.0技术
  17. 三菱PLC与上位机进行通讯
  18. ANSI字符集和Unicode字符集
  19. 大数据Clouder专项技能认证课程:Quick BI企业报表制作
  20. DFRobot行业AI开发者大赛--LattePandaDelta

热门文章

  1. 设计模式之里氏替换原则示例
  2. clearfix详解
  3. JDK包括什么?JRE包括什么?
  4. 一文彻底搞懂spring循环依赖
  5. 删除cookie之js实现
  6. 最完整的Springboot后台开发框架
  7. Non-local Neural Network
  8. cmdkey的方式修改windows凭据
  9. 小狐狸ChatGPT付费创作系统1.6.8独立开源版 + 公众号H5+小程序+VUE源码安装配置详细教程
  10. 【Flink】报错 No pooled slot available and request to ResourceManager for new slot failed