熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

基於Struts1.2的動態多文件上傳

2022-06-13   來源: Java開源技術 

  首先是webxml配置

  <?xml version= encoding=ISO ?>
    <!DOCTYPE webapp PUBLIC //Sun Microsystems Inc//DTD Web Application //EN app__dtd>

  <webapp>

  <servlet>
               <servletname>action</servletname>
               <servletclass>orgapachestrutsactionActionServlet</servletclass>
                   <initparam>
                     <paramname>config</paramname>
                     <paramvalue>/WEBINF/strutsconfigxml</paramvalue>
                   </initparam>
               <loadonstartup></loadonstartup>
           </servlet>

  <servletmapping>
               <servletname>action</servletname>
               <urlpattern>*do</urlpattern>
         </servletmapping>

  </webapp>
然後是strutsconfigxml

  <?xml version= encoding=UTF?>
    <!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

  <strutsconfig>
      <datasources />
      <formbeans >
        <formbean name=multiUploadForm type=comhbstrutsformMultiUploadForm />
      </formbeans>

  <globalexceptions />
      <globalforwards />
      <actionmappings >
        <action
          attribute=multiUploadForm
          name=multiUploadForm
          path=/multiUpload
          scope=request
          type=comhbstrutsactionMultiUploadAction
          validate=false />

  </actionmappings>
      <messageresources parameter=comhbstrutsApplicationResources />
    </strutsconfig>

  然後是頁面testjsp這段就是網上COPY的動態生成頁面

  <%@ page language=java pageEncoding=utf%>

  <%@ taglib uri=bean prefix=bean %>
    <%@ taglib uri=html prefix=html %>
    <%@ taglib uri=logic prefix=logic %>
    <%@ taglib uri=tiles prefix=tiles %>

  <!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>
    <html:html>
    <head>
    <title>
    multiUploadDemo
    </title>

  <script language=javascript>
     var num = ;

  function upload(){
      documentgetElementById(status)innerHTML = 文件上傳中;
         multiUploadFormsubmit();
       }

  function additem(id)
    {
     var rowcellstr;

  row = eval(documentall[++id++])insertRow();
     if(row != null )
        {
           cell = rowinsertCell();
           str=<input type=++file++ name=uploadFile[+ num +]file><input type=++button++ value=++刪除++ onclick=deleteitem(this++tb++);>
          cellinnerHTML=str;
        }
     num++;
    }
    function deleteitem(objid)
    {
     var rowNumcurRow;
     curRow = objparentNodeparentNode;
     rowNum = eval(documentall+id)rowslength ;
     eval(documentall[++id++])deleteRow(curRowrowIndex);
    }
    function callback(msg)
    {
     documentgetElementById(status)innerHTML = 文件上傳完成<br> + msg;
    }
    </script>
    <body bgcolor=#ffffff>
    <div id=status></div>
    <html:form method=post action=/multiUploaddo enctype=multipart/formdata target=hidden_frame>

  <table id=tb>
    </table>

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

  下面是FORM和ACTION


我把下面個文件放在FORM包中

  UploadFile java

  package comhbstrutsform;
    import javaioSerializable;

  import orgapachestrutsuploadFormFile;

  public class UploadFile implements Serializable {
      private  FormFile  file;
         public  FormFile  getFile()  {
                 Systemoutprintln(run  in  uploadFilegetFile());
                 return  file;
         }
         public  void  setFile(FormFile  file)  {
                 thisfile  =  file;
         }
    }

  MultiUploadForm java

  package comhbstrutsform;
    import javautilArrayList;
    import javautilList;

  import orgapachestrutsactionActionForm;

  import comhbstrutsUploadFile;

  public class MultiUploadForm extends ActionForm {
     private  List  myFiles;

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

  //注意這個方法的定義  不加中間的循環是會出錯的

  public  UploadFile  getUploadFile(int  index){
                int  size  =  myFilessize();
                if(index>size){
                 for(int i=;i<indexsize+;i++){
                        myFilesadd(new  UploadFile());
                 }
                }
                return  (UploadFile)myFilesget(index);
        }
        public  void  setMyFiles(List  myFiles)  {
                thismyFiles  =  myFiles;
        }

  }

  最後是action

  package comhbstrutsaction;

  import javaioIOException;
    import javaioPrintWriter;
    import javautilList;

  import javaxservlethttpHttpServletRequest;
    import javaxservlethttpHttpServletResponse;

  import orgapachestrutsactionAction;
    import orgapachestrutsactionActionForm;
    import orgapachestrutsactionActionForward;
    import orgapachestrutsactionActionMapping;
    import orgapachestrutsuploadFormFile;

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

  List myFiles = multiUploadFormgetMyFiles();
      String fileStr = ;
      for (int i = ; i < myFilessize(); i++) {
       UploadFile uploadFile = (UploadFile) myFilesget(i);
       FormFile file = uploadFilegetFile();

  if (file == null) {
        Systemoutprintln(file  is  null);
       } else {

  // 能運行到這裡就可以使用單個文件上傳的方法進行上傳了循環而已
        Systemoutprintln(filename>>>>>>>>>> + filegetFileName());
        Systemoutprintln(file  size>>>>>>>>>> + filegetFileSize());
        fileStr += filename::: + filegetFileName() + file  size:::
          + filegetFileSize();

  }
      }
// 返回文本
      try {
       responsesetHeader(ContentType text/html;charset=gbk);
       PrintWriter out = responsegetWriter();
       outwrite(<script>parentcallback(upload file success + fileStr
         + )</script>);
      } catch (IOException e) {
       eprintStackTrace();
      }

  return null;
     }

  }

  這個是我從現在寫好的裡截取的一部分可能還是有不正確的地方但是大體的思想是在這了 struts 我用的jar包


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28228.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.