批量下载例子

此例子是批量从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;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值