struts2文件上传与多文件上传

 

Struts2文件上传和多文件的上传

第一步:首先先写界面

</head>

        <div>

           <form action="upFiles.action" enctype="multipart/form-data" method="post"/>

           <div id="adds">

           上传文件:<input type="file" name="upload"/></br>

           </div>

           <input type="submit" value="上传"/>

           <input type="button" value="添加" id="but">

          

          

           </form>

       

        </div>

  <body>

  

  </body>

</html>

<script type="text/javascript">

<!--

$(document).ready(function(){

    $("#but").click(function (){

       var $add=$("<input type='file' name='upload'/></br>")

       var $text=$("<p>上传文件<p>");

       $("#adds").append($text);

       $("#adds").append($add);

   

    });

});

 

//-->

</script>

第二步

在Action类中添加以下属性

package cn.csdn.hr.action.uplaod;

 

import java.io.File;

import java.io.IOException;

import java.util.List;

 

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

import com.sun.net.httpserver.Authenticator.Success;

 

 

 

public class UploadListActions extends ActionSupport{

         //规定

         //文件内容

         private List<File> upload;

         //类

         private List<String> uploadContentType;

         //文件名称

         private List<String> uploadFileName;

         public List<File> getUpload() {

                   return upload;

         }

 

 

         public void setUpload(List<File> upload) {

                   this.upload = upload;

         }

 

 

         public List<String> getUploadContentType() {

                   return uploadContentType;

         }

 

 

         public void setUploadContentType(List<String> uploadContentType) {

                   this.uploadContentType = uploadContentType;

         }

 

 

         public List<String> getUploadFileName() {

                   return uploadFileName;

         }

 

 

         public void setUploadFileName(List<String> uploadFileName) {

                   this.uploadFileName = uploadFileName;

         }

 

        

        

         public String uploads(){

                  

                   String path =ServletActionContext.getServletContext().getRealPath("/im");

                   File file=new File(path);

                   if(!file.exists()){

                            file.mkdirs();

                           

                   }

 

                            try {

                                     for(int i=0;i<upload.size();i++){

                                              

                                               FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));

                                              

                                     }

                                              

                                              

                                              

                                              

                                    

                            } catch (IOException e) {

                                     // TODO Auto-generated catch block

                                     e.printStackTrace();

                            }

                  

                  

                   System.out.println("文件上传的名称"+getUploadFileName()+"上传的类型"+getUploadContentType());

                   return SUCCESS;

                  

                  

         }

 

 

        

}

第三步:struts.xml中添加配置

<package name="text" extends="struts-default" namespace="/">

   

   

    <action name="upFile" class="cn.csdn.hr.action.uplaod.UploadAction" method="upload">

    <result>./MyJsp.jsp</result>

   

    </action>

    <!--

   

     -->

     

     <action name="upFiles" class="cn.csdn.hr.action.uplaod.UploadListActions" method="uploads">

    <result>./MyJsp.jsp</result>

   

    </action>

     

    </package>

 

 

 

你可能感兴趣的:(struts,upload,File,Class,input,action)