程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 基於Struts1.2的動態多文件上傳

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

編輯:關於JAVA

首先是web.xml配置

<?xml version="1.0" encoding="ISO-8859-1" ?>
   <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
   <web-app>
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
          <init-param>
           <param-name>config</param-name>
           <param-value>/WEB-INF/struts-config.xml</param-value>
          </init-param>
        <load-on-startup>2</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
     </servlet-mapping>
   </web-app>

然後是struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
   <struts-config>
    <data-sources />
    <form-beans >
     <form-bean name="multiUploadForm" type="com.hb.struts.form.MultiUploadForm" />
    </form-beans>
    <global-exceptions />
    <global-forwards />
    <action-mappings >
     <action
      attribute="multiUploadForm"
      name="multiUploadForm"
      path="/multiUpload"
      scope="request"
      type="com.hb.struts.action.MultiUploadAction"
      validate="false" />
    </action-mappings>
    <message-resources parameter="com.hb.struts.ApplicationResources" />
   </struts-config>

然後是頁面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>

下面是FORM和ACTION

我把下面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;
     }
   }

最後是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;
   }
   }

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved