/**
* 合同批量下载
*
* @return
*/
@RequestMapping(value = "/cloudSign/downAll.htm", method = RequestMethod.POST)
public void downLoadAll(HttpServletRequest request, HttpServletResponse response) {
String cloudSignContract = request.getParameter("cloudSignContractVo");
List<CloudSignContractVo> list = JSON.parseArray(cloudSignContract, CloudSignContractVo.class);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd-HH:mm:ss");
String dateFormat = sdf.format(new Date());
String name = "";
String realZipName = "";
String zipName = dateFormat+"_协议" + ".zip";
String agent = request.getHeader("User-Agent"); // 获得浏览器头信息
realZipName = nameEncodeChanged(agent, zipName);
ZipOutputStream zipos = null;
DataOutputStream os = null;
try {
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + realZipName);
zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
zipos.setMethod(ZipOutputStream.DEFLATED);// 设置压缩方法
} catch (Exception e) {
e.printStackTrace();
logger.error("realZipName encodeed failed error :" + e);
}
for (int i = 0; i < list.size(); i++) {
try {
URL url = new URL(list.get(i).getUrl());
InputStream is = url.openStream();
name = list.get(i).getMemberName() + "_" + list.get(i).getMemberId() + ".pdf";
zipos.putNextEntry(new ZipEntry(name));
zipos.setEncoding("GBK");
os = new DataOutputStream(zipos);
byte[] b = new byte[1024];
int length = 0;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
zipos.closeEntry();
} catch (Exception e) {
e.printStackTrace();
logger.error("contract download failed error :" + e);
}
}
try {
os.flush();
os.close();
zipos.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("zipos close error :" + e);
}
}
/**
* 根据请求头类型判断浏览器类型,并对字符串选择编码
*
* @param agent 请求头
* @param fileName 文件名称
* @return 编码正确的文件名
* @throws UnsupportedEncodingException
*/
public String nameEncodeChanged(String agent, String fileName) {
String realZipName = "";
try {
if (agent.contains("MSIE") || agent.contains("Trident")) { // IE
realZipName = URLEncoder.encode(fileName, "UTF-8");
} else {
realZipName = new String(fileName.getBytes("utf-8"), "ISO8859-1"); // 其他
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
logger.error("nameEncodeChanged character error" + e);
}
return realZipName;
}