关于springmvc下服务器文件打包成zip格式下载功能
关于springmvc下服务器文件打包成zip格式下载功能
近期,项目要求把服务器存储的上传文件,批量下载到本地.参考网上资料,实现了服务器文件打包成压缩文件然后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;
}
关于springmvc下服务器文件打包成zip格式下载功能的更多相关文章
- java批量下载,将多文件打包成zip格式下载
现在的需求的: 根据产品族.产品类型,下载该产品族.产品类型下面的pic包: pic包是zip压缩文件: t_product表: 这些包以blob形式存在另一张表中: t_imagefile表: 现在 ...
- java批量将多文件打包成zip格式
public void createzip(){ List<File> nFileList = new ArrayList<File>(); nFileList.add(new ...
- java将文件打包成ZIP压缩文件的工具类实例
package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- java实现将文件压缩成zip格式
以下是将文件压缩成zip格式的工具类(复制后可以直接使用): zip4j.jar包下载地址:http://www.lingala.net/zip4j/download.php package util ...
- SpringMVC+Ajax实现文件批量上传和下载功能实例代码
需求: 文件批量上传,支持断点续传. 文件批量下载,支持断点续传. 使用JS能够实现批量下载,能够提供接口从指定url中下载文件并保存在本地指定路径中. 服务器不需要打包. 支持大文件断点下载.比如下 ...
- 多个文件下载打包生成zip格式下载
这个多个文件下载生成zip格式必须先引用一个ICSharpCode.SharpZipLib.dll. 代码如下 //将多个文件打包成压缩文件zip格式下载 protected voi ...
- Java实现文件自动打包成zip并下载的代码
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java. ...
- 文件打包为zip格式文件下载
整个思路是这样的: 1.查询数据库中的文件流放到datatable中2.循环datatable将文件流一个个生成文件,放到对应的文件夹中,3.下载某个文件夹下的所有文件a.循环这个文件夹下的所有文件, ...
- php 下载图片并打包成Zip格式压缩包
前言:最近公司有个需要下载多个图片并打包成压缩包的需求,下面来看看具体是怎么做的 1.没什么说的,懒得说啥,直接看代码 /** * 下载图片并生成压缩包 * @param $data 图片数组,一维 ...
随机推荐
- svn清理报错:Cleanup failed to process the following paths
这里碰到svn更新时,提示清理,清理时报错: 只需进行以下几个步骤即可解决:(原理即是清除掉svn数据库里的lock记录) 1.下载SQLiteManager,svn用的是sqlite数据库,需要一款 ...
- TW实习日记:第16天
前端的样式bug实在是太太太莫名其妙了,尤其是封装好的组件,一层套一层的,根本不知道是哪一层出了问题...除了改bug就是做新功能,真想吐槽一下这个项目的留言板,根本没人会用吧...这功能实在是太老旧 ...
- leetcode-打家劫舍(动态规划)
你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每 ...
- BOM / URL编码解码 / 浏览器存储
BOM 浏览器对象模型 BOM(Browser Object Model) 是指浏览器对象模型,是用于描述这种对象与对象之间层次关系的模型,浏览器对象模型提供了独立于内容的.可以与浏览器窗口进行互动的 ...
- 压力测试工具-webbench
简述 偶然情况下看到一款性能测试工具webbench,看着挺不错的记录一下安装过程,在以后项目上线过程中可以压一压一些页面的并发情况,对项目性能有个大致的了解. 原理 webbench首先fork出多 ...
- 三:QJM HDFS高可用
本文介绍的是HDFS的一种HA方案.虽然有checkpoint node \backup node等,但是不能实现自动的failover. http://hadoop.apache.org/docs/ ...
- Python练习—循环
1.输入n的值,求出n的阶乘. s=1 n = int(input("请输入一个数")) for i in range(1,n+1): s=s*i print(s) 2.折纸上月球 ...
- Notes of the scrum meeting(12.12)
meeting time:19:30~20:30p.m.,December 12th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- 20162328蔡文琛week09
学号 2016-2017-2 <程序设计与数据结构>第X周学习总结 教材学习内容总结 数据库是为了其他程序提供数据的应用软件. 关系书就哭通过唯一的标识符在不同表的记录见建立了关系. JD ...
- Oracle ORA-12541:TNS:no listener错误解决方法 (转)
前天装好的Oracle,昨天突然不好用了,从Oracle的错误提示来看,是说TNS:no listener ,估计是某种服务没有启动,打开windows管理工具->服务,一看,有一个Oracle ...