原理:
1.创建压缩流写到服务器上的压缩文件(临时文件)
2.把服务器上要下载的多个文件写入该压缩文件
3.把压缩文件通过输出流传输给客户端(注意:必须在zipoutputstream流关闭后,否则下载下的文件会报不可预料的压缩文件末端)
4.删除服务器上的临时文件

单文件

public void downImg(HttpServletResponse response,String filename,String path ){if (filename != null) {FileInputStream is = null;BufferedInputStream bs = null;OutputStream os = null;try {File file = new File(path);if (file.exists()) {//设置Headersresponse.setHeader("Content-Type","application/octet-stream");//设置下载的文件的名称-该方式已解决中文乱码问题response.setHeader("Content-Disposition","attachment;filename=" +  new String( filename.getBytes("gb2312"), "ISO8859-1" ));is = new FileInputStream(file);bs =new BufferedInputStream(is);os = response.getOutputStream();byte[] buffer = new byte[1024];int len = 0;while((len = bs.read(buffer)) != -1){os.write(buffer,0,len);}}else{String error = Base64Util.encode("下载的文件资源不存在");response.sendRedirect("/imgUpload/imgList?error="+error);}}catch(IOException ex){ex.printStackTrace();}finally {try{if(is != null){is.close();}if( bs != null ){bs.close();}if( os != null){os.flush();os.close();}}catch (IOException e){e.printStackTrace();}}}}

多文件下载:下载的是zip格式
前段js代码+后端controller代码

/*** 图片下载*/function downloadimg() {//获取所有被选的图片的名称与绝对路径放入数组var list = $(".activecheck");var imgNameList = [];var imgUrlList = [];for(var i = 0;i<list.length;i++){var b = $(list[i].childNodes[2]).attr("data-url");imgNameList.push(list[i].childNodes[3].innerText);//图片名称imgUrlList.push(b);//图片绝对路径}var paths =  encodeURI(encodeURI(imgUrlList));var names = encodeURI(encodeURI(imgNameList));//将名称传入后台window.location.href = "/imgUpload/imgdownload?names="+names+"&paths="+paths;}
  /*** 下载* @param response*/@RequestMapping(value = "/imgdownload", method = RequestMethod.GET)public void imgDownload(@SessionAttribute(MyInterceptor.SESSION_KEY) SessionInfo info,HttpServletResponse response,String [] names,String [] paths) {//存放--服务器上zip文件的目录String directory = "D:\\repository\\"+info.getName();File directoryFile=new File(directory);if(!directoryFile.isDirectory() && !directoryFile.exists()){directoryFile.mkdirs();}//设置最终输出zip文件的目录+文件名SimpleDateFormat formatter  = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");String zipFileName = formatter.format(new Date())+".zip";String strZipPath = directory+"\\"+zipFileName;ZipOutputStream zipStream = null;FileInputStream zipSource = null;BufferedInputStream bufferStream = null;File zipFile = new File(strZipPath);try{//构造最终压缩包的输出流zipStream = new ZipOutputStream(new FileOutputStream(zipFile));for (int i = 0; i<paths.length ;i++){//解码获取真实路径与文件名String realFileName = java.net.URLDecoder.decode(names[i],"UTF-8");String realFilePath = java.net.URLDecoder.decode(paths[i],"UTF-8");File file = new File(realFilePath);//TODO:未对文件不存在时进行操作,后期优化。if(file.exists()){zipSource = new FileInputStream(file);//将需要压缩的文件格式化为输入流/*** 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样这里的name就是文件名,* 文件名和之前的重复就会导致文件被覆盖*/ZipEntry zipEntry = new ZipEntry(realFileName);//在压缩目录中文件的名字zipStream.putNextEntry(zipEntry);//定位该压缩条目位置,开始写入文件到压缩包中bufferStream = new BufferedInputStream(zipSource, 1024 * 10);int read = 0;byte[] buf = new byte[1024 * 10];while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1){zipStream.write(buf, 0, read);}}}} catch (Exception e) {e.printStackTrace();} finally {//关闭流try {if(null != bufferStream) bufferStream.close();if(null != zipStream){zipStream.flush();zipStream.close();}if(null != zipSource) zipSource.close();} catch (IOException e) {e.printStackTrace();}}//判断系统压缩文件是否存在:true-把该压缩文件通过流输出给客户端后删除该压缩文件  false-未处理if(zipFile.exists()){downImg(response,zipFileName,strZipPath);zipFile.delete();}}

springboot-单文件多文件下载Zip相关推荐

  1. SpringBoot将文件打包成zip存放或导出

    目录 前言 环境准备 将文件打包成Zip存放 代码 测试 将文件打包成zip并导出 代码 测试 结尾 前言 相信各位看官在工作中都会遇到过要把多个文件打包成一个压缩文件然后导出,或者将文件打包成Zip ...

  2. Java实现文件下载zip包单文件等

    Java实现文件压缩包(zip)下载 摘要 本次主要记录将多个文件打包到zip压缩包并完成下载;留个代码方便以后用到了ctrl c v 1 多个文件打包成ZIP,下载zip包,单个文件下载 我使用的是 ...

  3. javaweb开发之处理表单上传文件和文件下载

    2019独角兽企业重金招聘Python工程师标准>>> 一.基于表单的上传文件 1. enctype属性 当表单需要上传文件时,需指定表单 enctype 的值为 multipart ...

  4. java post 多文件报头_Spring MVC-------文件上传,单文件,多文件,文件下载

    Spring MVC框架的文件上传是基于 commons-fileupload 组件的文件上传,只不过SpringMVC 框架在原有文件上传组件上做了进一步封装,简化了文件上传的代码实现,取消了不同上 ...

  5. SpringBoot多文件压缩包下载(多附件zip格式)

    文章目录 前言 : 此 Demo 为 Windows 环境下演示,部署到服务器的话路径需改成服务器的路径. 一.自定义工具类DownLoadZipUtil 二.Dao层分析与sql mapper层代码 ...

  6. SpringBoot实现文件单文件上传、批量上传、下载

    一.搭建一个SpringBoot框架 没有搭建的小伙伴,先去搭建一个 SpringBoot框架快速入门搭建Hello Worldhttps://blog.csdn.net/KangYouWei6/ar ...

  7. [WebApi]WebApi通过接口上传文件-单文件 多文件上传 文件下载

    WebApi通过接口上传文件 单文件上传(ajax,Form表单都适用) 1.html 2.javascript 3.C# Form表单之单文件上传 1.html 2.javascript 3.C# ...

  8. themyleaf 图片上传_javaEE --springboot #实现图片上传和回显 #单文件上传 #多文件上传 #ajax异步文件上传 (非常详细,从创建项目开始)...

    实现文件上传和回显 1.新建一个SpringBoot项目,选择 Spring Web 和 thymeleaf 依赖 .pow.xml文件下的依赖如下 2.根据下图,创建如下文件 3.直接上代码 配置文 ...

  9. springboot并发上传文件_springboot实现单文件和多文件上传

    本文实例为大家分享了springboot实现单文件/多文件上传的具体代码,供大家参考,具体内容如下 package com.heeexy.example.controller; import com. ...

  10. springboot文件上传,单文件上传和多文件上传,以及数据遍历和回显

    springboot文件上传,单文件上传和多文件上传 项目结构及pom.xml 创建文件表单页面 编写javabean 编写controller映射 MultipartFile类 @RequestPa ...

最新文章

  1. from beautifulsoup4 import BeautifulSoup 报错
  2. Notepad++中的高级查找
  3. hbase获取region以及读取每个region的第一行
  4. IOS的 testflight测试设置
  5. 故宫门票预订网站崩溃;岳云鹏信息打包卖 100 元;华为要做电视?| 极客头条...
  6. 安装Oracle Webcenter 11.1.1.8.0并使用JDeveloper开发Portlet
  7. 【区块链】区块链学习要点记录
  8. openwrt(路由器)的源码地址
  9. win10系统安装eplan2.7加密狗驱动蓝屏问题解决
  10. 自动登录163邮箱发送邮件(Python+Selenium)
  11. C# GDI+ 时钟表盘
  12. 深度学习中常见的打标签工具和数据集集合
  13. 土法炼钢:服务器定时汇报IP地址给我
  14. 处理 yarn 项目 has unmet peer dependency
  15. MaxEnt软件的下载与安装
  16. 教程 | 扁平物体的摄影测量重建方案
  17. BIOS 和 EFI 启动光盘制作
  18. EM78P153B封装SOP8单片机方案IC开发
  19. 每周分享第 60 期
  20. edge for android 6.0,终于等到你 三星S6/S6 Edge迎来Android 6.0

热门文章

  1. 企业内部短信模板大全分析
  2. 分享干货——数控加工中常用的三种补偿方法
  3. 染色基础知识(四)——怎么染?
  4. 致我们终将远离的子女
  5. 统计学+变异+变异系数
  6. Android新浪微博开发(一)授权认证
  7. linux 字符 拨号上网,LINUX下用ADSL拨号上网
  8. iOS集成twitter分享
  9. 如何缓解眼疲劳(眼疲劳敷眼睛是热敷还是冷敷)
  10. Java调用IE浏览器