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 ...
随机推荐
- 零基础学redis
第一个阶段:redis基本知识了解: 1. redis的百度百科解释: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言 ...
- [FJSC2014]异或之
[题目描述] 给定n个非负整数A[1], A[2], ……, A[n]. 对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n ...
- BZOJ1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
1609: [Usaco2008 Feb]Eating Together麻烦的聚餐 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 938 Solved ...
- Unity3d 获取屏幕depth与normal
Depth 获取Depth的几种方法,分别有不同效果 1. <span style="font-size:14px;"> float2 depth ...
- JavaScript高级程序设计7.pdf
function类型 每个函数都是function类型的实例,函数是对象,函数名是指向对象的指针 function sum(num1,num2) { return num1+num2; } //等价于 ...
- Matlab编程-基本命令行语句
(1) mathlab命令行中“,”与“:”的区别: 结尾不加任何东西也会输出结果 以“,”结尾不显示变量数值,但是再次输入变量名之后可以输出变量值 以“:”结尾显示变量值 (2) 输出格式控制 ...
- poj 3678 Katu Puzzle(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
- postgresql数据库的数据导出
一.pg_dump的用法:数据库的导入导出是最常用的功能之一,每种数据库都提供有这方面的工具,例如Oracle的exp/imp,Informix的dbexp/dbimp,MySQL的mysqldump ...
- linux 监控命令
先总结下常用的一些监控工具: ##linux命令 w 系统负载 lsof -p pid 进程打开的文件 lsof -i:port 端口的运行情况 free -m 内存情况 vmstat 进程.内存.内 ...
- hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区 ...