日期:2014-05-16 浏览次数:20700 次
下载 :
? 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); } } } }
?