DBOperation.java中的一个方法
public void alluponefile(String filesb, HttpServletRequest request, String typeid, String secondid, String levelid) { DBOperate dbo = new DBOperate(); String filesbs = filesb.replace("//", "/"); // filesbs+sub[i] int lastseparator = filesbs.lastIndexOf("/"); String dirfilesb = filesbs.substring(0, lastseparator); File dir = new File(dirfilesb); HttpSession session = request.getSession(); manager manager1 = (manager) session.getAttribute("manager"); String sub[] = dir.list(); for (int i = 0; i < sub.length; i++) { if (new File(dirfilesb + "/" + sub[i]).isFile())// 是文件就添加 {
int lastdot = sub[i].lastIndexOf("."); String title = sub[i].substring(0, lastdot); String content = sub[i]; String filesrc = dirfilesb + "/" + sub[i]; String format = sub[i].substring(lastdot + 1); if (format.equals("rar") || format.equals("doc") || format.equals("zip")) // 限制上传文件的格式 { Source_inf inf = new Source_inf();// 向数据库中插入一条条的数据 try { inf.setFilesrc(new String(filesrc.getBytes("gb2312"), "iso-8859-1")); inf.setFormats(format); inf.setSizes(2 + "KB"); // 本地上传的文件不知道大小 inf.setFilename(new String(title.getBytes("gb2312"), "iso-8859-1")); if (!typeid.equals("")) { inf.setTypeid(PubFunc.parseInt(typeid)); } else { if (!secondid.equals("")) { inf.setTypeid(PubFunc.parseInt(secondid)); } else { inf.setTypeid(PubFunc.parseInt(levelid)); }
} inf.setContent(new String(content.getBytes("gb2312"), "iso-8859-1")); inf.setDatetime(new java.util.Date().toLocaleString()); inf.setState("1"); inf.setNewss("1"); inf.setRedian("0"); inf.setAuthor(new String(manager1.getUsername() .getBytes("gb2312"), "iso-8859-1")); inf.setNumbers(1); inf.setShenhe(1); dbo.insert_update(inf, "insert");
}
catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
}
else // 是目录就 { String infilesb = (dirfilesb + "/" + sub[i] + "/234.rar") .replace("//", "/"); alluponefile(infilesb, request, typeid, secondid, levelid);//自己调用自己,递归 }
}
}
使用
public ActionForward uploadallsource(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { DBOperate dbo = new DBOperate(); String levelid = request.getParameter("levelid"); String secondid = request.getParameter("secondid"); String typeid = request.getParameter("typeid"); //重点:文件夹的名字传 String filesb=request.getParameter("filesb"); String filesbs=filesb.replace("//","/"); //filesbs+sub[i] int lastseparator=filesbs.lastIndexOf("/"); String dirfilesb=filesbs.substring(0, lastseparator); dbo.alluponefile(filesb,request,typeid,secondid,levelid); Log log1 = new Log(); HttpSession session = request.getSession(); manager manager1=(manager)session.getAttribute("manager"); log1.setUsername(manager1.getUsername()); log1.setDatetime(new java.util.Date().toLocaleString()); log1.setAction(new String(("批量添加下载资源<font color='#FF0000'><strong>" + new String(dirfilesb.getBytes("iso-8859-1"),"gbk") + "</strong>--" + "</font>目录下的文件成功!").getBytes("gbk"),"iso-8859-1")); dbo.insert_update(log1, "insert"); PrintWriter out=response.getWriter(); out.println("<script>"); out.println("alert('add success!!');"); out.println("window.location.href='/web/admin/allup.jsp'"); out.println("</script>"); // response.sendRedirect("/web/admin/allup.jsp"); return actionMapping.findForward("");
}
jsp页面 allup.jsp <form name="form6" method="post" action="/web/center/SourceAction.html?method=uploadallsource"> <tr class="tab2"> <td width="15%" height="0" ><div align="right"id="upload_uptext" style="margin-top:5px;"><span class="STYLE6">选择要批量上传的文件夹中的任一个文件:</span></div></td> <td width="80%" class="paddcont"> <input type="file" id="f1" style="display:none"> <input id="f2" name="filesb"><input type="button" value="本地文件夹" onClick="f1.click();f2.value=f1.value;"> * </td> </tr> <tr class="tab1"> <td height="0" class="contfont"><div align="right"id="upload_uptext" style="margin-top:5px;"><span class="STYLE6">资料分类:</span></div></td> <td > <select name="levelid" onChange="selectSecondType(0)"> <option value="" selected>-- 请选择 --</option> <%for(int i=0;i<typelistFirst.size();i++){ Source_type sourcetype=(Source_type)typelistFirst.get(i); %> <option value="<%=sourcetype.getId()%>"><%=new String(sourcetype.getTypename().getBytes("iso-8859-1"),"gb2312")%></option> <%}%> </select>* <select name="secondid" style="display:none" onChange="selectThreeType(0)"> <option value="" selected>-- 请选择 --</option> </select> <select name="typeid" style="display:none"> <option value="" selected>-- 请选择 --</option> </select></td> </tr> <tr> <td> </td> <td><input id="tijiaofiles" type="button" value="确认提交信息" onClick="javascript:return checksubmit()"></td> </tr> </table> </form>
<script> var secondidsize=new Array();; var threesize=new Array(); //选择二级分类 function selectSecondType(s) { var a = document.getElementsByName("levelid"); switch (a[s].value) { case'': var type_labels=new Array("-- 请选择 --"); var type_values=new Array(""); break; <% for(int i = 0; i < typelistFirst.size(); i++) { Source_type typeone = (Source_type)typelistFirst.get(i); out.println("case'"+typeone.getId()+"':"); //System.out.println("case'"+typeone.getId()+"':"); //二级分类信息 List typelistSecond=(List)dbo.findSQLHibernate("from Source_type as type1 where type1.levels='"+typeone.getId()+"' and state='1' order by type1.orders desc",1); out.println("var type_labels = new Array();"); out.println("var type_values = new Array();"); out.println("type_labels[0] = /"-- 请选择 --/";"); out.println("type_values[0] = /"/";"); out.println("secondidsize[s]="+typelistSecond.size()); for(int j=0;j<typelistSecond.size();j++) { Source_type Type = (Source_type)typelistSecond.get(j); out.println("type_labels["+(j+1)+"] = /""+new String(Type.getTypename().getBytes("iso-8859-1"),"gb2312")+"/";"); out.println("type_values["+(j+1)+"] = /""+Type.getId()+"/";"); } out.println("break;"); } %> } var b = document.getElementsByName("secondid"); b[s].options.length = 0; for(var i = 0; i <type_labels.length; i++) { b[s].add(document.createElement("OPTION")); if(i>0){ b[s].style.display=""; }else{ b[s].style.display="none"; } b[s].options[i].text=type_labels[i]; b[s].options[i].value=type_values[i]; } }
var nms=0;
//选择三级分类 function selectThreeType(s){ var b = document.getElementsByName("secondid"); switch (b[s].value) { case'': var product_labels=new Array("-- 请选择 --"); var product_values=new Array(""); break; <% for(int i = 0; i < typelistFirst.size(); i++) { Source_type typeone = (Source_type)typelistFirst.get(i); List typeList = (List)dbo.findSQLHibernate("from Source_type as type1 where type1.levels='"+typeone.getId()+"' and state='1' order by type1.orders desc",1); for(int j=0;j<typeList.size();j++) { Source_type Type = (Source_type)typeList.get(j); out.println("case'"+Type.getId()+"':"); //三级分类 List threeList=null; threeList=(List)dbo.findSQLHibernate("from Source_type as type1 where type1.levels='"+Type.getId()+"' and state='1' order by type1.orders desc",1); out.println("var product_labels = new Array();"); out.println("var product_values = new Array();"); out.println("product_labels[0] = /"-- 请选择 --/";"); out.println("product_values[0] = /"/";"); out.println("threesize[s]="+threeList.size()); for(int k = 0; k < threeList.size(); k++) { Source_type Producttype = (Source_type)threeList.get(k);; out.println("product_labels["+(k+1)+"] = /""+new String(Producttype.getTypename().getBytes("iso-8859-1"),"gb2312")+"/";"); out.println("product_values["+(k+1)+"] = /""+Producttype.getId()+"/";"); } out.println("break;"); } } %> } var c = document.getElementsByName("typeid"); c[s].options.length = 0; for(var i = 0; i <product_labels.length; i++) { c[s].add(document.createElement("OPTION")); if(i>0){ c[s].style.display=""; }else{ c[s].style.display="none"; } c[s].options[i].text=product_labels[i]; c[s].options[i].value=product_values[i]; } }
function checkspace(checkstr) { var str = ''; for(i = 0; i < checkstr.length; i++) { str = str + ' '; } return (str == checkstr); }
function checksubmit(){ if(checkspace(form6.levelid.value)){ alert('请选择分类!'); form6.levelid.focus(); return false; } if(secondidsize>0){ if(checkspace(form6.secondid.value)){ alert('请选择分类!'); form6.secondid.focus(); return false; } } if(threesize>0){ if(checkspace(form6.typeid.value)){ alert('请选择分类!'); form6.typeid.focus(); return false; } } if(checkspace(form6.filesb.value)){ alert('请选择资料!'); form6.filesb.focus(); return false; } form6.submit(); }
</script>