zip格式压缩文件并打包下载
Map<String, String> param = new HashMap<String, String>();
SimpleDateFormat df = new SimpleDateFormat("yyyy");
try {
getResponse().setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("文件名.zip", "UTF-8"));
ZipOutputStream zos = new ZipOutputStream(getResponse().getOutputStream());
BufferedOutputStream bos = new BufferedOutputStream(zos);
List<Map<String, Object>> requriments = new ArrayList<Map<String,Object>>();
for (Map<String, Object> map : requriments) {
List<Map<String,Object>> attachmentList = new ArrayList<Map<String,Object>>();
boolean nofile = false;
String dirName = df.format(df.parse(map.get("createDate").toString()))+"/"+map.get("deptName").toString()+"/"+map.get("num").toString()+"/";
for (Map<String, Object> attmp : attachmentList) {
if (attmp != null) {
File file = new File(attmp.get("diskPath") + File.separator
+ attmp.get("diskDirectory") + File.separator
+ attmp.get("fileSaveName"));
if (file.exists()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file) );
zos.putNextEntry(new ZipEntry(dirName+file.getName().substring(
file.getName().indexOf("_") + 1).replaceAll(" ", "")));
int len = 0;
byte [] buf = new byte[10 * 1024];
while((len = bis.read(buf,0,buf.length)) != -1) {
bos.write(buf,0,len);
}
bis.close();
bos.flush();
nofile = true;
}
}
}
if(!nofile) {
zos.putNextEntry(new ZipEntry(dirName));
bos.flush();
}
}bos.close();
} catch (Exception e) {
e.printStackTrace();
getRequest().setAttribute("message", "文件不存在或已被锁定,下载失败!!");
return null;
} finally {
}
return null;