ajaxはダウンロードの検証とダウンロードをします

4550 ワード

ダウンロード:
jspページ
<%String productId = request.getParameter("productId") == null ? "" : request.getParameter("productId");%>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY topmargin="10px">

<form action="" name="downloadstrufile">
	<input type="hidden" name="packageId1" value="<%=productId%>">
</form>
</BODY>
<script language="javascript">
<%--      --%>
function forStruDown(){
    var url="<%=request.getContextPath()%>/compressFilesForm/new_downloadCompressedFile_validate.so";
    var param="packageId1=<%=productId%>";
	var myAjax=new Ajax.Request(url,{method:'post',parameters:param,onSuccess:doStruDown,asynchronous:true});
}
var doStruDown=function(text){
	var returnvalue = text.responseText;
	if(returnvalue == "1"){
		var url = "<%=request.getContextPath()%>/compressFilesForm/new_downloadCompressedFile.so";
		document.forms["downloadstrufile"].action = url;
		document.forms["downloadstrufile"].submit();
	}else if(returnvalue == "0"){
			alert("     !");
	}
}
</script>
</HTML>

バックグラウンドjava
           
  public void new_downloadCompressedFile_validate(WebContext context) {
		HttpServletRequest request = (HttpServletRequest)context.get(WebcConstant.Param_Request);
		
		FileInputStream in = null;
		String flag = "1";
				try {
			String packageId = getParameter("packageId1", context);
			List daoList = searchFileType(packageId);
						
			String sql = " ";
			List<String>  files = hibernateDao.executeSqlQuery(sql);
			for (String file : files) {
			in = new FileInputStream(dataFilePath +"temp/zip/"+ file);				
			}
		} catch (Exception e) {
			flag ="0";
		} 
		HttpServletResponse rep = WebcUtils.getResponse(context);
		ForwardSpecification fs = new ForwardSpecification();
		fs.setType(ForwardView.ajaxType);
		ForwardView view = new SimpleForwardViewImpl();
		try {
			view.forward(flag, request, rep, fs);
		} catch (Exception e) {
		}
		finally {
			if (in != null) {
				try {
					in.close();
					in = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

 
     // 
  public void new_downloadCompressedFile(WebContext context) {
		if("yes".equals(getParameter("all", context))){
			downloadCompressedFileByObject(context);
			return;
		}
		FileInputStream in = null;
		try {
			HttpServletResponse response = WebcUtils.getResponse(context);
			String packageId = getParameter("packageId1", context);
			String sql = "" ;
			List<String>  files = hibernateDao.executeSqlQuery(sql);
			
			String file = SpoilageMgrUtils.notEmptyStr(files);//    
			if(StringUtils.isNotBlank(file)){
				String exportedFileName =file;
			in = new FileInputStream(dataFilePath +"temp/zip/"+ file);				
				//        
				response.reset();
				response.setContentType("text/html;charset=GBK");
				response.addHeader("Content-Disposition", "attachment; filename="
						+ java.net.URLEncoder.encode(exportedFileName, "UTF-8"));
				//          
				byte[] b = new byte[1024];
				int len;
				while ((len = in.read(b)) > 0) {
					response.getOutputStream().write(b, 0, len);
				}
				response.getOutputStream().flush();
				response.getOutputStream().close();
			}
		} catch (Exception e) {
			SgLoger.writeErrorInfo(this.getClass(), "new_downloadCompressedFile", e);
		} finally {
			if (in != null) {
				try {
					in.close();
					in = null;
				} catch (Exception e) {
					SgLoger.writeErrorInfo(this.getClass(), "new_downloadCompressedFile", e);
				}
			}
		}
	}