servlet实现多文件打包下载
当用户一次下载多个文件时。普通情况是,每下载一个文件,均要弹出一个下载的对话框。这给用户造成了非常大不便。
比較理想的情况是,用户选择多个文件后。server后端直接将多个文件打包为zip。以下贴出实现代码。
前端Javascript代码(使用Javascript创建表单。通过提交表单的方式訪问后端的MultiDownload):
var tmpForm = document.createElement("form");
tmpForm.id = "form1" ;
tmpForm.name = "form1" ;
document.body.appendChild(tmpForm);
var tmpInput = document.createElement("input");
// 设置input对应參数
tmpInput.type = "text";
tmpInput.name = "fileUrls" ;
tmpInput.value = downloadUrls;
// 将该输入框插入到 tmpform 中
tmpForm.appendChild(tmpInput);
tmpForm.action= "servlet/MultiDownload" ;
tmpForm.method= "get";
tmpForm.submit();
//从html从移除该form
document.body.removeChild(tmpForm);
后端MultiDownload代码:
public class MultiDownload extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MultiDownload() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String strArray = null;
strArray = request.getParameter("fileUrls");
//传递到servlet时,默认将编码转换为ISO-8859-1,将其又一次转码为GBK
strArray=new String(strArray.getBytes("ISO-8859-1"), "GBK");
String[] filePathArray = strArray.split(",");
String zipFileName = "product.zip";
response.setContentType("application/x-msdownload" ); // 通知客户文件的MIME类型:
response.setHeader("Content-disposition" , "attachment;filename=" + zipFileName);
//要下载的文件文件夹
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
for (String filePath : filePathArray) {
File file = new File(filePath);
doZip(file, zos);
}
zos.close();
}
/**
* 打包为 zip 文件
* @param file 待打包的文件
* @param zos zip zip输出流
* @throws IOException
*/
private void doZip(File file, ZipOutputStream zos)
throws IOException {
if(file.exists()) {
if (file.isFile()) {
//假设是文件,写入到 zip 流中
zos.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
zos.flush();
fis.close();
} else {
//假设是文件夹。
递归查找里面的文件
String dirName = file.getName() + "/";
zos.putNextEntry(new ZipEntry(dirName));
File[] subs = file.listFiles();
for (File f : subs) {
makeZip(f, dirName, zos);
}
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
servlet实现多文件打包下载的更多相关文章
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- PHP 多文件打包下载 zip
<?php $zipname = './photo.zip'; //服务器根目录下有文件夹public,其中包含三个文件img1.jpg, img2.jpg, img3.jpg,将这三个文件打包 ...
- flask BytesIO() 多个文件打包下载 zipfile
使用zipfile模块可以将多个文件打包成zip文件进行下载,但是常规的操作方式会在服务器磁盘上生成一个zip文件占用磁盘空间. 后引入BytesIO将文件写入到内存中然后下载: def dl_pla ...
- java 实现多文件打包下载
jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.acti ...
- Java批量文件打包下载
经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,但是在打包过程中,常常也会出现下载过来的zip文件中里面有乱码的文件名, ...
- de4dot3.14更新文件打包下载
刚发现de4dot更新了,虽然只是10月份的文件更新,并未发布新的release,但好多人还不会编译... 关于de4dot有何功能就不再讲了. 本文主要提供编译通过后的打包文件下载. 首先下载de4 ...
- Java批量文件打包下载zip
网上看了很多,本文使用ant.jar中的org.apache.tools.zip,页面用js表单提交 代码供参考: ACTION: /* * 另存为 */ @RequestMapping(" ...
- 【Java】Java批量文件打包下载zip
网上看了很多,本文使用ant.jar中的org.apache.tools.zip,页面用js表单提交 代码供参考: ACTION: /* * 另存为 */ @Request ...
- 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip
<?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...
随机推荐
- node.js发http请求
标准库中默认的HTTP模块 const https = require('https'); https.get('https://api.nasa.gov/planetary/apod?api_key ...
- iconfont补遗
一.TureTpe(.ttf)格式: .ttf字体是Windows和Mac的最常见的字体,是一种RAW格式,因此他不为网站优化,支持这种字体的浏览器有[IE9+,Firefox3.5+,Chrome4 ...
- Hadoop中HDFS工作原理
转自:http://blog.csdn.net/sdlyjzh/article/details/28876385 Hadoop其实并不是一个产品,而是一些独立模块的组合.主要有分布式文件系统HDFS和 ...
- 关于Unity中的transform组件(二)
在Scene视图中的蓝色网格,每一格默认是1米 一.沿着Z轴每秒移动10米 Transform cube_trans; void start(){ this.cube_trans=this.trans ...
- 002servlet生命周期以及有关servlet的各种知识
4 Sevlet的生命周期(重点) 有关servlet的类有Servlet,HttpServlet以及GenericServlet. 其实我们要写一个Servlet只要写一个类去实现Servet就可以 ...
- Angular入门篇高速开发导航网
简单介绍 AngularJS 是一个为动态WEB应用设计的结构框架,提供给大家一种新的开发应用方式.这样的方式能够让你扩展HTML的语法.以弥补在构建动态WEB应用时静态文本的不足.从而在web应用程 ...
- 基于docker部署的微服务架构(四): 配置中心
原文:http://www.jianshu.com/p/b17d65934b58%20 前言 在微服务架构中,由于服务数量众多,如果使用传统的配置文件管理方式,配置文件分散在各个项目中,不易于集中管理 ...
- cocos2dx --- 富文本的使用 RichText
在实际工作中,有非常多地方会使用 富文本,这里仅仅介绍最简单的富文本用法: 是由cocostudio 提供的 RichText: 直接贴代码,再分析: //这里測试富文本控件 ui::RichText ...
- C# 导出Excel "正在中止线程" 错误
导出Excel相信很多人都用过,但是我却遇到了一个问题 “正在中止线程” 源代码如下: public static void ExportExcel(string fileName, GridView ...
- malloc free, new delete 的异同点
相同点: 都可以动态的申请并释放内存 不同点: 1. 用法不同 <1> malloc 函数为 void* malloc(size_t size), 用于申请一块长度为 size 字节的内存 ...