关于springmvc下服务器文件打包成zip格式下载功能

2016年09月21日 11:22:14 toxic_guantou 阅读数:5731更多

个人分类: 技术点存储

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/toxic_guantou/article/details/52605920

近期,项目要求把服务器存储的上传文件,批量下载到本地.参考网上资料,实现了服务器文件打包成压缩文件然后down到本地功能.
具体代码实现:
1、在服务器端创建一个临时zip格式文件
2、用jdk自带的API将服务器所有文件输入到zip文件中
3、将zip文件下载到本地,并删除服务器端zip文件

@RequestMapping(value = "/downloadZip.do")public String downloadFiles(String tcLwIds, HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { List<File> files = new ArrayList<File>(); File Allfile = new File(request.getSession().getServletContext().getRealPath("/") + "upload/"); if (Allfile.exists()) { File[] fileArr = Allfile.listFiles(); for (File file2 : fileArr) { files.add(file2); } } String fileName = UUID.randomUUID().toString() + ".zip"; // 在服务器端创建打包下载的临时文件 String outFilePath = request.getSession().getServletContext().getRealPath("/") + "upload/"; File fileZip = new File(outFilePath + fileName); // 文件输出流 FileOutputStream outStream = new FileOutputStream(fileZip); // 压缩流 ZipOutputStream toClient = new ZipOutputStream(outStream); // toClient.setEncoding("gbk"); zipFile(files, toClient); toClient.close(); outStream.close(); this.downloadFile(fileZip, response, true); return null; }

将服务器文件存到压缩包中

public static void zipFile(List<File> files, ZipOutputStream outputStream) throws IOException, ServletException { try { int size = files.size(); // 压缩列表中的文件 for (int i = 0; i < size; i++) { File file = (File) files.get(i); zipFile(file, outputStream); } } catch (IOException e) { throw e; } } public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, ServletException { try { if (inputFile.exists()) { if (inputFile.isFile()) { FileInputStream inStream = new FileInputStream(inputFile); BufferedInputStream bInStream = new BufferedInputStream(inStream); ZipEntry entry = new ZipEntry(inputFile.getName()); outputstream.putNextEntry(entry); final int MAX_BYTE = 10 * 1024 * 1024; // 最大的流为10M long streamTotal = 0; // 接受流的容量 int streamNum = 0; // 流需要分开的数量 int leaveByte = 0; // 文件剩下的字符数 byte[] inOutbyte; // byte数组接受文件的数据 streamTotal = bInStream.available(); // 通过available方法取得流的最大字符数 streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分开的数量 leaveByte = (int) streamTotal % MAX_BYTE; // 分开文件之后,剩余的数量 if (streamNum > 0) { for (int j = 0; j < streamNum; ++j) { inOutbyte = new byte[MAX_BYTE]; // 读入流,保存在byte数组 bInStream.read(inOutbyte, 0, MAX_BYTE); outputstream.write(inOutbyte, 0, MAX_BYTE); // 写出流 } } // 写出剩下的流数据 inOutbyte = new byte[leaveByte]; bInStream.read(inOutbyte, 0, leaveByte); outputstream.write(inOutbyte); outputstream.closeEntry(); // Closes the current ZIP entry // and positions the stream for // writing the next entry bInStream.close(); // 关闭 inStream.close(); } } else { throw new ServletException("文件不存在!"); } } catch (IOException e) { throw e; } }

下载文件

 public void downloadFile(File file,HttpServletResponse response,boolean isDelete) {try { // 以流的形式下载文件。 BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("UTF-8"),"ISO-8859-1")); toClient.write(buffer); toClient.flush(); toClient.close(); if(isDelete) { file.delete(); //是否将生成的服务器端文件删除 } } catch (IOException ex) { ex.printStackTrace(); } } 

单个文件的下载

@RequestMapping(value="/singleDownload.do")public String singleDownload(String fileName, HttpServletRequest request, HttpServletResponse response)throws Exception{ response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName="+ fileName); String realPath = request.getSession().getServletContext().getRealPath("/"); String path = realPath+"upload/"; File file = new File(path+ File.separator + fileName); downloadFile(file, response, false); return null; }

转载于:https://www.cnblogs.com/libin6505/p/9970672.html

关于springmvc下服务器文件打包成zip格式下载功能相关推荐

  1. python打包zip文件_python 解压文件,合并文件 打包成zip格式文件 生成MD5值

    #!/usr/bin/env python #_*_encoding:utf-8 # 2018/05/29 #augustyang #2.0 ''' 解压文件,合并文件 打包成zip格式文件 生成MD ...

  2. Java将多个文件打包成ZIP并下载

    Java将多个文件打包成ZIP并下载 需求是多个文件需要同时打包成zip压缩文件并下载到本地,首先我需要的是知道下载文件的路径.我有一个专门的sys_file_info表,表中有对应的文件路径.业务表 ...

  3. php 生成zip并下载,PHP 实现文件打包成zip格式并下载

    PHP 文件打包并下载 有个这样的需求,将多个文件打包成zip格式并下载到本地 可根据 ZipArchive这个类来实现此功能 我自己也研究了一下,然后把搞出来了 ,经测试绝对好用 话不多说直接上代码 ...

  4. php 复制文件夹并压缩到最小_php压缩多个文件打包成zip并下载到本地

    完成时间:2018-01-03 展现方式:整个相册图片的导出(下载到本地)直接在页面上生成下载没有什么另存为什么默认路径 基本步骤: 1.用户点击按钮跳转到对应相册导出的方法 ---->2.在方 ...

  5. spring mvc java 把多文件打包成zip,并下载

    再来一篇 /*** 压缩并导出文件* @param zipPath 压缩文件临时路径 路径最后不要有 /* @param zipName 压缩为文件名 **.zip* @param createFil ...

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

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

  7. Java向将指定文件(含文件列表)或者指定路径下目录打包成zip格式

    Java向将指定文件(含文件列表)或者指定路径下目录打包成zip格式 运行环境 pom依赖 测试方法 测试效果 运行环境 1.springboot 2.2.x 2.maven 3.5.4 pom依赖 ...

  8. think.class.php下载,PHP_ThinkPHP实现将本地文件打包成zip下载,首先,将FileToZip.class文件放到T - phpStudy...

    ThinkPHP实现将本地文件打包成zip下载 首先,将FileToZip.class文件放到ThinkPHP/Extend/Library/ORG/Util/文件夹中,FileToZip.class ...

  9. 文件打包成zip类型文件

    研究了一下,如何把文件打包成Zip文件. 下面是我经历的进程: 1. 首先看到了微软本身的打包代码,是通过System.IO.Packaging 命令空间来进行打包,你可以点击以上的链接来查看其如何打 ...

最新文章

  1. 如何写出安全的API接口(参数加密+超时处理+私钥验证+Https)
  2. javascript数组降维_Javascript实现的数组降维——维度不同,怎么谈恋爱
  3. 人工智能影响未来娱乐的31种方式
  4. C 一个非递减数组 下标从0到n 元素的取值范围为从0到n的整数 判断其中是否有重复元素
  5. 陕西中小企业促进局 e-mail_重点注意2020陕西工程师职称评审破格指南
  6. zuk z2 Android7.0官方,首发ZUK Z2官方固件ZUI2.5安卓7.0卡刷包!
  7. 无广告的pdf阅读器_奥利给!免费无广告!功能超齐全!这样的良心国产软件,真的不多了!...
  8. origin对数据进行操作
  9. 马斯克回应“逃税”:没有从特斯拉领取高薪 一直在按规定缴税
  10. 应对大数据分析的几个方法
  11. 【笔记】线性代数的本质
  12. 计算机一级安装包怎么升级,详细教您win7如何升级为sp1
  13. 【Jectpack】DataStore
  14. DHT11温湿度传感器(zigbee)
  15. git clone报错error: RPC failed; curl 56 GnuTLS recv error (-110): The TLS connection was non-properly
  16. 电商API接口如何使用(教你获取数据)
  17. php 判断百度蜘蛛抓取,判断百度蜘蛛偷偷进行转移权重301,给新站提权
  18. java数组的四种拷贝方式
  19. java 代码佛像_论面向组合子程序设计方法 之九 南无阿弥陀佛
  20. 刚写完的 基于微信的房产中介预约看房小程序 毕业设计毕设源码

热门文章

  1. Linux系统调用的运行过程【转】
  2. 2016全球数据新闻奖(DJA)颁布, 12个获奖作品全剖析
  3. Android 双击和手势的图片缩放
  4. 会声会影如何渲染高清视频
  5. UCOS 操作系统 安装配置环境
  6. 申请英国学校最晚什么时候考出雅思呢?
  7. 静态函数一个有用的设计模式
  8. TypeSprict -- 基础类型
  9. XebiaLabs DevOps平台推出软件发布风险和合规性管理功能
  10. re:Invent解读:没想到你是这样的AWS