此例子是批量从ftp下载到指定目录然后打包写回到respose里
前台是用js拼一个临时form提交,ftp地址是已知的
后台包括从ftp下载,由于业务需要从两个ftp下载,下载完之后打包,打包的可以直接用
js代码(包括全选):
// 下载页面上选择的证书
function downloadAllEcert(){
var boxes = $("input[name='_checkbox']:checked");
var urls = new Array();
if (boxes) {
var arr = new Array();
for (var i = 0; i < boxes.length; i++ ) {
if (boxes[i].checked) {
var as = $(boxes[i]).parent().parent().find("a");
if (as) {
for (var j = 0; j < as.length; j++ ) {
var h = $(as[j]).attr("href");
urls.push(encodeURIComponent(h));
}
}
}
}
}
if (urls.length > 0) {
var url = "${ctx}/sq/batchDownloadCerts.do";
var html = "<form method='POST' action='"+url+"' >";
html += " <input type='hidden' value='"+JSON.stringify(urls)+"' name='list'/>";
html += "</form>";
$(html).appendTo('body').submit().remove();
} else {
alert("没有可下载的证书");
}
}
java代码:
public String batchDownloadCerts(){
String urls = this.getRequest().getParameter("list");
if (StringUtil.isNotEmpty(urls)) {
String[] urlArr = urls.replace("[", "").replace("]", "").replaceAll("\"", "").split(",");
if (urlArr != null && urlArr.length > 0) {
// 获取pdf文件目录
String pdfPath = ServletActionContext.getServletContext().getRealPath("/pdf");
// 创建临时目录
String filename = System.currentTimeMillis() +"";
File f = new File(pdfPath + File.separator + filename + File.separator);
if (!f.exists()) {
f.mkdirs();
}
for (String u : urlArr) {
try {
String url = URLDecoder.decode(u, "utf-8");
System.out.println(url);
Map<String, String> map = getQueryString(url);
String system = map.get("system");
String ftpPath = map.get("file");
String realFileName = map.get("name");
// system 为ecert_ftp 则为电子证书 否则为普通证书
FtpUtils ftp = new FtpUtils();
String hzName = ftpPath.substring(ftpPath.lastIndexOf("."), ftpPath.length());
String fileNameAndPath = f.getPath() + File.separator + realFileName + hzName;
if (new File(fileNameAndPath).exists()) {
fileNameAndPath = f.getPath() + File.separator + realFileName + "_" + new SimpleDateFormat("SSS").format(new Date()) + hzName;
}
if (system != null && "ecert_ftp".equals(system)){
DbLogin dblogin =dicSsmisService.geDbLogin(system);
ftp.downloadFile(dblogin, ftpPath, fileNameAndPath);
} else {
ftp.downFile(ftpPath, fileNameAndPath, system);
}
} catch (Exception e) {
e.printStackTrace();
}
}
try {
// 打包
String zipFilePathName = pdfPath + File.separator + filename + ".zip";
ZipCompressorByAnt ant = new ZipCompressorByAnt(zipFilePathName);
ant.compress(f.getPath());
// 删除临时目录
if (f.exists()) {
FileUtil.delAllFile(f.getAbsolutePath());
}
// 打完包输出到response
int readBytes = 0;
ServletOutputStream sout = null;
InputStream in = null;
HttpServletResponse response = getResponse();
sout = response.getOutputStream();
String agent = (String)getRequest().getHeader("USER-AGENT");
String realName = filename + ".zip";
if (agent != null && (agent.indexOf("MSIE") != -1 || (agent.indexOf("like Gecko") != -1))) {// IE
String enableFileName = new String(realName.getBytes("GBK"), "ISO-8859-1");
response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
} else {
String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(realName.getBytes("UTF-8")))) + "?=";
response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
}
File zipFile = new File(zipFilePathName);
in = new FileInputStream(zipFile);
//read from the file; write to the ServletOutputStream
while ((readBytes = in.read()) != -1)
sout.write(readBytes);
in.close();
sout.close();
// 删除压缩包
if (zipFile.exists()) {
// zipFile.delete();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}