/**
*前面页面采用a标签,ajax成功后打开页面指定项目中的下载压缩文件的方法
* window.open(contextPath+"/html/html_downloadZipFile.do?fileName="+encodeURIComponent(id+"/"+fileName));
* 从项目中的文件夹中下载压缩文件
* @throws Exception
*/
public void downloadZipFile() throws Exception{
String downloadPath = getRequest().getSession().getServletContext()
.getRealPath("/")+ "download/";
//获取get方法参数
ActionContext context = ActionContext.getContext();
Map<String, Object> parameterMap = context.getParameters();
String fileNameArray[] = (String[]) parameterMap.get("fileName");
String fileName = fileNameArray[0];
//解码
fileName = java.net.URLDecoder.decode(fileName,"UTF-8");
//设置编码
getRequest().setCharacterEncoding("UTF-8");
//指定需要下载压缩包的名字
String downloadName = fileName+".zip";
getResponse().setContentType("multipart/form-data");
//压缩包的路径
String zipFilePath = getRequest().getSession().getServletContext()
.getRealPath("/")+ "download/" + downloadName;
FileInputStream fin = new FileInputStream(zipFilePath);
downloadName = URLEncoder.encode(downloadName, "UTF-8");
getResponse().setHeader("Content-Disposition", "attachment;fileName=" + downloadName);
getResponse().setContentLength(fin.available());
OutputStream out = getResponse().getOutputStream();
byte[] buf = new byte[8*1024];
int len;
while ((len = fin.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
out.close();
fin.close();
//文件工具类,删除项目中download目录中的临时文件
FileUtil.delAllFile(downloadPath);
}