首先是ActionForm代码:

 

import java.util.ArrayList;
import java.util.List;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UploadManyForm extends ActionForm {
 private static final long serialVersionUID = -4609151398021079963L;
 private List<FormFile> myfile=new ArrayList<FormFile>();
 
 public FormFile getMyfile(int index) {
  return myfile.get(index);
 }

 public void setMyfile(int index, FormFile formFile) {
  this.myfile.add(formFile);
 }

 public List<FormFile> getFiles(){
  return myfile;
 }
}

其次是Action中代码:

 

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
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;

import com.wubangjie.movie.util.Constant;
import com.wubangjie.movie.web.form.UploadManyForm;

public class UploadManyAction extends Action {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  UploadManyForm uploadManyForm = (UploadManyForm) form;
  List<FormFile> files = uploadManyForm.getFiles();
  String root = this.getServlet().getServletContext().getRealPath("\\");
  List<String> fileNames=new ArrayList<String>();
  for (FormFile formFile : files) {
    //得到文件名
   String filename=Constant.UPLOAD_PATH+"\\"+formFile.getFileName();
   fileNames.add(filename);
   write(formFile, root);
  }
  request.setAttribute("path", fileNames);
  request.setAttribute("fileName", fileNames);
  return null;
 }

 /**
  * 按照给定的路径写入指定的文件
  *
  * @param file
  *            需要写入的文件
  * @param path
  *            写入的路径
  * @throws IOException
  *             如果写入出错,则抛出此异常
  */
 public void write(FormFile file, String path) throws IOException {
  if (file.getFileSize() == 0) {
   return;
  }
  InputStream stream = file.getInputStream();
  OutputStream bos = new FileOutputStream(path + "\\"
    + Constant.UPLOAD_PATH + "\\" + file.getFileName());
  int bytesRead = 0;
  byte[] buffer = new byte[8192];
  while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
   bos.write(buffer, 0, bytesRead);
  }
  bos.close();
  stream.close();
 }

}

 

在JSP代码:

 

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

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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
 <html:base />

 <title>上传多个文件</title>

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script language="javascript" type="text/javascript">
      
          var i=0;
          function addInput(){
            if(i>=0){
                  var attach = "myfile[" + i + "]";
                  if(createInput(attach))
                      i=i+1;
              }
             
          }
         
          function deleteInput(){
                  if(i>=1){
                    i=i-1;
                    if(!removeInput())
                        i=i+1;
                }
          }
         
          function createInput(nm){  
                var p=document.createElement("div");
              var aElement=document.createElement("input");  
             aElement.name=nm;
             aElement.id=nm;
             aElement.type="file";
             aElement.size="50";
             p.appendChild(aElement);
             if(document.getElementById("upload").appendChild(p) == null)
             {
                      return false;
                      }
               return true;
          }
   

          function removeInput(nm){
               var aElement = document.getElementById("upload");
                if(aElement.removeChild(aElement.lastChild) == null)
                    return false;
                return true;  
          }
   
    function check(){
            var aElement = document.getElementById("upload");
              var index = aElement.children.length;
              var fso=new ActiveXObject("Scripting.FileSystemObject");
              var filesize=0;
              while(index>0){
             
              var s = aElement.children[index-1].children[0].value;
             
              if(s==''){
              alert('请为'+i+'个输入选择文件');
              return false ;
              }else{
              var f=fso.GetFile(s);
              filesize=filesize+f.size
              }
              index = index-1;
          }
          if(filesize>15728640){
          alert('所传文件总大小不能超过15M');
          return false;
          }
          return true ;
       }
    </script>

</head>

<body>
 <div align="center">
  上传多个文件
 </div>
 <center>
  <html:form action="fileUpload.do" method="post"
   enctype="multipart/form-data" onsubmit="return check()">
   <span id="upload"></span>
   <input type="button" name="button" value="添加附件" onclick="addInput()">&nbsp;&nbsp;
            <input type="button" name="button" value="删除附件"
    onclick="deleteInput()">
   <br>
   <font color=red>上传文件总大小不能超过15M</font>
   <br>
   <html:submit value="确定上传" />
   <html:cancel />
  </html:form>
 </center>

 


</body>
</html:html>

祝你成功!

posted on 2008-11-20 19:41  吴邦杰  阅读(875)  评论(0)    收藏  举报