基于Struts1.2的动态多文件上传

test.jsp

<!--这段就是网上COPY的动态生成页面-->

    <%@ page language="java" pageEncoding="utf-8"%>

    <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html:html>
    <head>
    <title>
    multiUploadDemo
    </title>

    <script language="javascript">
     var num = 0;

     function upload(){
      document.getElementById("status").innerHTML = "文件上传中...";
         multiUploadForm.submit();
       }

    function additem(id)
    {
     var row,cell,str;

     row = eval("document.all["+'"'+id+'"'+"]").insertRow();
     if(row != null )
        {
           cell = row.insertCell();
           str="<input type="+'"'+"file"+'"'+" name=uploadFile["+ num +"].file><input type="+'"'+"button"+'"'+" value="+'"'+"删除"+'"'+" onclick='deleteitem(this,"+'"'+"tb"+'"'+");'>"
          cell.innerHTML=str;
        }
     num++;
    }
    function deleteitem(obj,id)
    {
     var rowNum,curRow;
     curRow = obj.parentNode.parentNode;
     rowNum = eval("document.all."+id).rows.length - 1;
     eval("document.all["+'"'+id+'"'+"]").deleteRow(curRow.rowIndex);
    }
    function callback(msg)
    {
     document.getElementById("status").innerHTML = "文件上传完成...<br>" + msg;
    }
    </script>
    <body bgcolor="#ffffff">
    <div id="status"></div>
    <html:form method="post" action="/multiUpload.do" enctype="multipart/form-data" target="hidden_frame">

    <table id="tb">
    </table>

    </html:form>
    <iframe name='hidden_frame' id="hidden_frame" style="display:none"></iframe>
    <input type="button" name="btnAddFile" value="Add File" onclick="additem('tb')"/>
    <input type="button" name="btnUpload" value="upload" onclick="upload()"/>
    </body>
    </html:html>

2.form

我把下面2个文件放在FORM包中

    UploadFile .java

    package com.hb.struts.form;
    import java.io.Serializable;

    import org.apache.struts.upload.FormFile;

    public class UploadFile implements Serializable {
      private FormFile file;
         public FormFile getFile() {
                 System.out.println("run in uploadFile.getFile()");
                 return file;
         }
         public void setFile(FormFile file) {
                 this.file = file;
         }
    }

    MultiUploadForm .java

    package com.hb.struts.form;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.struts.action.ActionForm;

    import com.hb.struts.UploadFile;

    public class MultiUploadForm extends ActionForm {
     private List myFiles;

        public MultiUploadForm(){
                myFiles = new ArrayList();
                myFiles.add(new UploadFile());
        }
        public List getMyFiles() {
                return myFiles;
        }

    //注意这个方法的定义 不加中间的循环是会出错的

      public UploadFile getUploadFile(int index){
                int size = myFiles.size();
                if(index>size-1){
                 for(int i=0;i<index-size+1;i++){
                        myFiles.add(new UploadFile());
                 }
                }
                return (UploadFile)myFiles.get(index);
        }
        public void setMyFiles(List myFiles) {
                this.myFiles = myFiles;
        }


    }

3.action

package com.hb.struts.action;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;

    public class MultiUploadAction extends Action {
     public ActionForward execute(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {
      MultiUploadForm multiUploadForm = (MultiUploadForm) form;

      List myFiles = multiUploadForm.getMyFiles();
      String fileStr = "";
      for (int i = 0; i < myFiles.size(); i++) {
       UploadFile uploadFile = (UploadFile) myFiles.get(i);
       FormFile file = uploadFile.getFile();

       if (file == null) {
        System.out.println("file is null");
       } else {

        // 能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而已
        System.out.println("filename>>>>>>>>>>" + file.getFileName());
        System.out.println("file size>>>>>>>>>>" + file.getFileSize());
        fileStr += "filename:::" + file.getFileName() + "file size:::"
          + file.getFileSize();

       }
      }
      // 返回文本
      try {
       response.setHeader("ContentType", "text/html;charset=gbk");
       PrintWriter out = response.getWriter();
       out.write("<script>parent.callback('upload file success" + fileStr
         + "')</script>");
      } catch (IOException e) {
       e.printStackTrace();
      }

      return null;
     }

    }
转自:http://hi.baidu.com/yanglu_168/blog/item/12c1382a4bc35028d42af1bd.html

posted on 2010-06-25 09:26 飞熊 阅读(871) 评论(0)  编辑  收藏 所属分类: 上传下载


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年6月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜