/**
* 批量下载
* @param request
* @param response
* @return void
*/
@RequestMapping("/PolicyController!batchFileDownload")
public void batchFileDownload(HttpServletRequest request,HttpServletResponse response){
// 1. 批量下载多个文件时,系统将所有文件打包成压缩文件,命名规则:政策类型(当前时间),时间精确到时分秒,如“月度商务政策(20130910111501)”
// 2. 下载功能受权限控制;
// 3. 单次下载文件不得超过200M,否则提示用户;
//政策分类(1:月度政策查询; 2:年度政策查询;3:产品手册查询;4:售后政策查询;5:其他政策查询;)
//String policyCategory = request.getParameter("category");
String policyCategory = request.getParameter("category");
if(policyCategory == null || "".equals(policyCategory)){
policyCategory = "1";
}
//policyCategory = "2";
//文件名称(数字名称)
String filePaths = request.getParameter("filePaths");
//filePaths = "201310141018309019.pdf,201310140940426989.pdf";
String[] filePathSplt = filePaths.split(",");
// String fileName = request.getParameter("");
//fileName = "比亚迪-201310-B-关于开展(科技十年十重大礼)全线跨年盛惠营销活动的通知.pdf,花样百出城.xml";
// String[] fileNameSplt = fileName.split(",");
String businessPolicyPath = PolicyConstants.policyDownloadPath;
//临时目录(文件夹)
String tempPath = request.getSession().getServletContext().getRealPath("/")+"policy_temp\\";
File[] downloadFiles = new File[filePathSplt.length];
for(int i=0;i<filePathSplt.length;i++){
String chineseName = policyService.updateFileNameAndgetFileChineseNameByNumName(tempPath, businessPolicyPath, filePathSplt[i]);
if(!"".equals(chineseName)){
downloadFiles[i] = new File(chineseName);
}
/***********start*********增加文件下载数据(用于判断此文件下载状态)*******************/
//当前sessionID
String sessionId = request.getSession().getId();
Date nowTime = new Date();
String path = PolicyConstants.policyDownloadPath + filePathSplt[i];
File file = new File(path);
//文件大小(单位:字节(B)) B-->M,B/1024/1024 = M
long fileSize = file.length();
DownloadFileInfo info = new DownloadFileInfo();
info.setFileName(chineseName.substring(chineseName.lastIndexOf("\\")+1));
info.setFilePath(filePathSplt[i]);
info.setSessionId(sessionId);
info.setInsertTime(nowTime);
info.setFileSize(fileSize);
policyService.insertDownloadFileData(info);
/***********end*********增加文件下载数据(用于判断此文件下载状态)*******************/
}
DownloadUtil util = new DownloadUtil();
//文件名称(精确到毫秒)
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String zipName = "";
if("1".equals(policyCategory)){
zipName = PolicyConstants.monthPolicyName;
}else if("2".equals(policyCategory)){
zipName = PolicyConstants.yearPolicyName;
}else if("3".equals(policyCategory)){
zipName = PolicyConstants.productManualName;
}else if("4".equals(policyCategory)){
zipName = PolicyConstants.afterSaleyPolicyName;
}else if("5".equals(policyCategory)){
zipName = PolicyConstants.otherPolicyName;
}
//.zip文件名称
String zipFileName = zipName + "(" + sdf.format(new Date()) + ")";
//zip(无数据)文件绝对目录
String path = request.getSession().getServletContext().getRealPath("/")+"policy_temp\\"+zipFileName+".zip";
//zip(有数据)文件绝对目录
String zipPath = "";
try {
zipPath = util.getZipPath(downloadFiles, path);
} catch (Exception e1) {
e1.printStackTrace();
}
File filepatch = new File(zipPath);
//文件大小(单位:字节(B)) B-->M,B/1024/1024 = M
long fileSize = filepatch.length();
String overLimitSizeTip = "";
InputStream is = null;
OutputStream fileOut = null;
if (filepatch.exists()) {
try {
is = new FileInputStream(zipPath);
response.setContentType("application/zip;charset=UFT-8");
response.setHeader("Content-Disposition","attachment; filename=" + PolicyUtils.toUtf8String(request,zipFileName+".zip"));
fileOut = response.getOutputStream();
byte[] dd = new byte[1024];
while(is.read(dd, 0, dd.length) != -1){
fileOut.write(dd);
}
fileOut.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fileOut!=null){
try {
fileOut.close();
} catch (Exception e) {}
}
if(is!=null){
try {
is.close();
} catch (Exception e) {}
}
//删除每个临时文件
for(int jj=0;jj<downloadFiles.length;jj++){
if (downloadFiles[jj].exists()) {
downloadFiles[jj].delete();
}
}
//删除zip文件
if (filepatch.exists()) {
filepatch.delete();
}
}
}
}