struts1 多个文件下传和上载,jquery
struts1 多个文件上传和下载,jquery
struts1 多个文件上传和下载,jquery
<html:form styleId="myform1" action="/FwrzyshList.do" method="post" enctype="multipart/form-data"> <table id="table6" cellpadding="0" cellspacing="0" border="1" width="90%" align="center" onkeydown="if(event.keyCode==13)event.keyCode=9"> <tr> <td colspan="3" class="tab"> <table class="tabs" cellpadding="0" cellspacing="0"> <tr> <td class=tab_sf></td> <td class=tab_f> 附件 </td> <td class=tab_ef></td> <td class=tab_ef align="right"> <input type="button" class="button" onclick="addTable6()" value="增加"> </td> </tr> </table> </td> </tr> <tr> <th width="40%"> 文件名称 </th> <th width="30%"> 上传时间 </th> <th> 操作 </th> </tr> <logic:notEmpty name="fwrzxysPersistentForm" property="dtywFwrzxysFjPOList"> <logic:iterate id="data" name="fwrzxysPersistentForm" indexId="index" property="dtywFwrzxysFjPOList"> <tr onMouseOver="this.style.backgroundColor='#FFCCCC'" onMouseOut="this.style.backgroundColor=''" > <td> <bean:write property="wjmc" name="data" /> <!-- 文件名称 --> </td> <td> <bean:write property="mrut" name="data" /> <!-- 上传时间 --> </td> <td> <a onclick="deleteTable2(this,'<bean:write property="ysbh" name="data" />','<bean:write property="xh" name="data" />','t6')" style="cursor: hand">删除</a> </td> </tr> </logic:iterate> </logic:notEmpty> <tr id='tr6'> <td colspan='2' align='center'> <input type='file' name='file' size='25' /> </td> <td> <a onclick="deleteTable0(this)" style="cursor: hand">删除</a> </td> </tr> </table> </html:form>
var $tr6; $(document).ready(function() { $tr6 = $("#tr6").remove(); }); function addTable6() { $tr6.clone().appendTo("#table6"); } function toSaveYsd() { if (!checkNull()) { return; } $("#table6 input[type='file']").each(function() { III++; this.name="file"+III; }); var myform=$("#myform1").serialize(); var params = decodeURIComponent(myform,true); document.forms[0].action = "/fwrzxysPersist.do?method=save&"+params; document.forms[0].submit(); }
var $tr6; $(document).ready(function() { $tr6 = $("#tr6").remove(); }); function addTable6() { $tr6.clone().appendTo("#table6"); } function toSave() { $("#table6 input[type='file']").each(function() {//一定要有这一步,否则只能上传一个文件 III++; this.name="file"+III; }); //var myform=$("#myform1").serialize(); //var params = decodeURIComponent(myform,true); document.forms[0].action = "/aa.do?method=save&"+params; document.forms[0].submit(); }
Action 后面代码:
Hashtable files = fwrzxysPersistentForm .getMultipartRequestHandler().getFileElements(); for (Enumeration e = files.keys(); e.hasMoreElements();) { FormFile file = (FormFile) files.get((String) e.nextElement()); if (file != null && file.getFileSize() > 0) { String fileName = file.getFileName(); InputStream is = file.getInputStream(); String patch = ((FwrdwbaPath) Config.getBean("fwrbafj")) .getFilePath(); File f = new File(patch); if (!f.exists()) { f.mkdirs(); } SimpleDateFormat sf = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss"); Date date = new Date(); String name = requestContext.getCzydm() + sf.format(date) + "" + fileName; File fil = new File(patch, name); FileOutputStream fos = new FileOutputStream(fil); byte[] b = new byte[1024]; int real = 0; real = is.read(b); while (real > 0) { fos.write(b, 0, real); real = is.read(b); } fos.close(); is.close(); file.destroy(); iv++; DtywFwrzxysFjPO dtywFwrzxysFjPO = new DtywFwrzxysFjPO(); dtywFwrzxysFjPO.setYsbh(ysbh); dtywFwrzxysFjPO.setXh(iv + ""); dtywFwrzxysFjPO.setScwjlj(name); dtywFwrzxysFjPO.setWjmc(fileName); dtywFwrzxysFjPO.setJgdm(requestContext.getJgdm()); dtywFwrzxysFjPO.setXxly(xxly); new DtywFwrzxysFjDAO().add(dtywFwrzxysFjPO); } }
-----------以上为上传, 下面为下载文件-------------------------
javascript
function download(ysbh, xh, lx) { window.open("/aa.do?method=downLoad&ysbh=" + ysbh + "&xh=" + xh + "&lx=" + lx); }
Action
/** * 下载附件的上传文件 * * @return */ public ActionForward downLoad(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws java.net.SocketException { String ysbh = request.getParameter("ysbh"); String xh = request.getParameter("xh"); String lx = request.getParameter("lx"); BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream fos = null; InputStream fis = null; String filepath = null; String fileName = ""; try { if (lx.equalsIgnoreCase("fj")) {// 文件下载 FjPO PO = new FjDAO().get( ysbh, xh); fileName = PO.getWjmc(); filepath = ((BaPath) Config.getBean("bafj")) .getFilePath() + File.separator + PO.getScwjlj().trim(); } File uploadFile = new File(filepath); fis = new FileInputStream(uploadFile); bis = new BufferedInputStream(fis); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); // 弹出下载对话框时防止乱码 response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8")); int bytesRead = 0; // 用输入流进行先读,然后用输出流去写,使用的是缓冲输入输出流 byte[] buffer = new byte[8192]; while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.flush(); fis.close(); bis.close(); fos.close(); bos.close(); } catch (java.net.SocketException e1) { logger.error("文件下载失败" + e1); e1.printStackTrace(); } catch (Exception e) { logger.error("文件下载失败" + e); e.printStackTrace(); } return null; }