Java批量文件打包下载zip
网上看了很多,本文使用ant.jar中的org.apache.tools.zip,页面用js表单提交
代码供参考:
ACTION:
/*
* 另存为
*/
@RequestMapping("/saveAs.do")
public @ResponseBody
void saveAs(String filePath, String fileName) { try {
File file = new File(filePath);
// 设置文件MIME类型
getResponse().setContentType(getMIMEType(file));
// 设置Content-Disposition
getResponse().setHeader(
"Content-Disposition",
"attachment;filename="
+ URLEncoder.encode(fileName, "UTF-8"));
// 获取目标文件的绝对路径
String fullFileName = getRealPath("/upload/" + filePath);
// System.out.println(fullFileName);
// 读取文件
InputStream ins = new FileInputStream(fullFileName);
// 放到缓冲流里面
BufferedInputStream bins = new BufferedInputStream(ins);
// 获取文件输出IO流
// 读取目标文件,通过response将目标文件写到客户端
OutputStream outs = getResponse().getOutputStream();
BufferedOutputStream bouts = new BufferedOutputStream(outs);
// 写文件
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 这里一定要调用flush()方法
ins.close();
bins.close();
outs.close();
bouts.close();
} catch (Exception e) {
e.printStackTrace();
}
} /*
* 批量下载另存为
*/
@RequestMapping("/batDownload.do")
public @ResponseBody
void batDownload(String filePaths, String fileNames) {
String tmpFileName = "work.zip";
byte[] buffer = new byte[1024];
String strZipPath = getRealPath("/upload/work/"+tmpFileName);
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
strZipPath));
String[] files=filePaths.split("\\|",-1);
String[] names=fileNames.split("\\|",-1);
// 下载的文件集合
for (int i = 0; i < files.length; i++) {
FileInputStream fis = new FileInputStream(getRealPath("/upload/"+files[i]));
out.putNextEntry(new ZipEntry(names[i]));
//设置压缩文件内的字符编码,不然会变成乱码
out.setEncoding("GBK");
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
saveAs("work/"+tmpFileName, tmpFileName); } catch (Exception e) {
e.printStackTrace();
}
} /**
* 根据文件后缀名获得对应的MIME类型。
*
* @param file
*/
private String getMIMEType(File file) {
String type = "*/*";
String fName = file.getName();
// 获取后缀名前的分隔符"."在fName中的位置。
int dotIndex = fName.lastIndexOf(".");
if (dotIndex < 0) {
return type;
}
/* 获取文件的后缀名 */
String end = fName.substring(dotIndex, fName.length()).toLowerCase();
if (end == "")
return type;
// 在MIME和文件类型的匹配表中找到对应的MIME类型。
for (int i = 0; i < MIME_MapTable.length; i++) {
if (end.equals(MIME_MapTable[i][0]))
type = MIME_MapTable[i][1];
}
return type;
} private final String[][] MIME_MapTable = {
// {后缀名, MIME类型}
{ ".doc", "application/msword" },
{ ".docx",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
{ ".xls", "application/vnd.ms-excel" },
{ ".xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
{ ".pdf", "application/pdf" },
{ ".ppt", "application/vnd.ms-powerpoint" },
{ ".pptx",
"application/vnd.openxmlformats-officedocument.presentationml.presentation" },
{ ".txt", "text/plain" }, { ".wps", "application/vnd.ms-works" },
{ "", "*/*" } };
};
<form id="batForm" action="<%=path%>/file/batDownload.do" method="post">
<input type="hidden" id="filePaths" name="filePaths" value=""/>
<input type="hidden" id="fileNames" name="fileNames" value=""/>
</form>
function download(){
var objs=$("#fileFrame").contents().find("input[name='ckFile']:checked");
if(objs.length>0){
var filePaths="";
var fileNames="";
for(var i=0;i<objs.length;i++){
filePaths+=$("#fileFrame").contents().find("#path_"+objs[i].value).val()+"|";
fileNames+=$("#fileFrame").contents().find("#a_"+objs[i].value).html()+"|";
}
filePaths=filePaths.substring(0,filePaths.length-1);
fileNames=fileNames.substring(0,fileNames.length-1);
$("#filePaths").val(filePaths);
$("#fileNames").val(fileNames);
$("#batForm").submit();
}else{
alert("请选择需要下载的文件!");
return false;
}
}
Java批量文件打包下载zip的更多相关文章
- 【Java】Java批量文件打包下载zip
网上看了很多,本文使用ant.jar中的org.apache.tools.zip,页面用js表单提交 代码供参考: ACTION: /* * 另存为 */ @Request ...
- Java批量文件打包下载
经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,但是在打包过程中,常常也会出现下载过来的zip文件中里面有乱码的文件名, ...
- java将文件打包成ZIP压缩文件的工具类实例
package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- PHP 多文件打包下载 zip
<?php $zipname = './photo.zip'; //服务器根目录下有文件夹public,其中包含三个文件img1.jpg, img2.jpg, img3.jpg,将这三个文件打包 ...
- 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip
<?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...
- 几行代码轻松实现PHP文件打包下载zip
<?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- 关于springmvc下服务器文件打包成zip格式下载功能
关于springmvc下服务器文件打包成zip格式下载功能 2016年09月21日 11:22:14 toxic_guantou 阅读数:5731更多 个人分类: 技术点存储 版权声明:本文为博主 ...
- flask BytesIO() 多个文件打包下载 zipfile
使用zipfile模块可以将多个文件打包成zip文件进行下载,但是常规的操作方式会在服务器磁盘上生成一个zip文件占用磁盘空间. 后引入BytesIO将文件写入到内存中然后下载: def dl_pla ...
随机推荐
- 搭建Tornado+Nginx
Tornado一个高效的异步非阻塞式的实时Web服务器,是Facebook旗下的 FriendFeed 网站开源Web服务器版本.但是它内置的HTTP服务器功能有限,不能在生产环境下使用. 在 Fri ...
- LightOJ_1248 Dice (III)
题目链接 题意: 给一个质地均匀的n的骰子, 求投掷出所有点数至少一次的期望次数. 思路: 这就是一个经典的邮票收集问题(Coupon Collector Problem). 投掷出第一个未出现的点数 ...
- sqlserver 学习
http://www.cnblogs.com/CareySon/category/411344.html SQL Server查找的最小单位实际上是页.也就是说即使你只查找一行很小的数据,SQL Se ...
- BZOJ 1026 windy数
Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? In ...
- ftp 匿名访问设置
为了让ftp可以匿名访问,需要设置/etc/vsftp.conf 的 anonymous_enable=YES. 当然仅仅是这样,还是不可以的,会出现错误: vsftpd: refusing to r ...
- R语言 典型相关分析
1.关键点 #典型相关分析##典型相关分析是用于分析两组随机变量之间的相关程度的一种统计方法,它能够有效地揭示两组随机变量之间的相互(线性依赖)关系#例如 研究生入学考试成绩与本科阶段一些主要课程成绩 ...
- 利用MVC的过滤器实现url的参数加密和解密
最近在与一个IOS应用做接口对接,之前一直都没有遇到什么很大的问题,但是有一天发现可以通过软件解析app的url,然后直接通过url的拼接修改接口数据,这一下使得数据的安全性和准确性都降低了,于是就想 ...
- Linux学习笔记6——映射虚拟内存
使用到两个函数mmap:用于分配内存:munmap:用于释放内存. mmap的原型如下: void *mmap( void *start, //指定映射的虚拟地址 0由系统指定开始位置 ...
- intellij idea 2016版破解方法
之前办法不能用了,现在最新方法,打开http://idea.lanyus.com,直接获取验证码即可
- CXF Service Interceptor请求,响应报文之控制台输出
一:定义接口 @WebService(targetNamespace = "http://www.unionpay.com/client/appprovider", name = ...