smartupload--文件批量下载
来源:互联网 发布:ps软件字体下载 编辑:程序博客网 时间:2024/05/16 04:35
jsp页面
<h1>批量下载</h1>
<form action="batchDownloadServlet">
<input type="checkbox" name="filename" value="img1-lg.jpg">img1
<input type="checkbox" name="filename" value="img3-thumb.jpg">img3
<input type="checkbox" name="filename" value="img2-thumb.jpg">img2
<input type="submit" value="下载">
</form>
servlet控制器
package com.imooc.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BatchDownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置响应头信息
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;filename=test.zip");
//设置根路径
String path = getServletContext().getRealPath("/")+"images/";
//获取要下载文件的数组
String[] filenames = request.getParameterValues("filename");
String str ="";
String rt ="\r\n";
//Zip压缩要下载的文件
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
//循环读取每个文件写入到ZipOutputStream中
for(String filename : filenames){
str += filename+rt;
//得到文件
File file = new File(path+filename);
//设置需要压缩的下一个文件条目
zos.putNextEntry(new ZipEntry(filename));
//读取需要下载的文件内容
FileInputStream fis = new FileInputStream(file);
//读取文件,循环写入到压缩流当中
byte[] b = new byte[4096];
int len = 0;
while((len = fis.read(b)) != -1){
zos.write(b, 0, len);
}
zos.flush();
fis.close();
}
zos.setComment("download success:"+rt+str);
zos.flush();
zos.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
- smartupload--文件批量下载
- 文件批量上传--smartUpload
- smartupload下载
- 文件批量下载
- SmartUpload处理下载
- SmartUpload 文件下载
- smartupload实现文件下载
- 文件下载--smartupload
- php文件批量下载
- struts2实现文件批量下载
- java文件批量打包下载
- Java 文件批量下载
- 文件批量打包下载
- 文件批量打包下载
- smartupload 上传和下载文件
- 文件批量打包下载 框架springmvc
- 文件批量上传和打包下载
- SmartUpload
- kotlin-cn front end
- 设计模式之迭代器模式的学习思考
- STM32固件库详解
- 多线程Join函数小案例
- JAVA解析TXT分割标题章节正文后写入数据库(某公司机试题以及关键源代码)
- smartupload--文件批量下载
- BaseDao多线程的实现
- python3.6下安装(numpy,scipy,pandas,matplotlib,scikit-learn)
- JAVA中的进程和线程
- 多线程 BaseDao
- POJ 1837 Balance 笔记
- 【原创】【组合数学】vijos-1629 八(容斥原理+最小公倍数)
- Maven项目提示:Plugin execution not covered by lifecycle configuration
- 多线程BackGround小案例