Struts2 File Upload

后端 未结 2 439
挽巷
挽巷 2020-12-20 02:34

I want to upload multiple files in one form and that number of files is varying from user to another, ex. a user may upload 2 files and another user could upload 12 files. I

相关标签:
2条回答
  • 2020-12-20 03:07

    You can use build in Struts2 multiple file uploading feature.In your Jsp file you have to define something like

    <s:form action="doMultipleUploadUsingList" method="POST" enctype="multipart/form-data">
        <s:file label="File (1)" name="upload" />
        <s:file label="File (2)" name="upload" />
        <s:file label="FIle (3)" name="upload" />
        <s:submit />
    </s:form>
    

    which will send these files to Your action class and you have the option of Collecting the file content as a list

    public class MultipleFileUploadUsingListAction extends ActionSupport {
    
        private List<File> uploads = new ArrayList<File>();
        private List<String> uploadFileNames = new ArrayList<String>();
        private List<String> uploadContentTypes = new ArrayList<String>();
        // There getter and setter methods
    
    }
    

    here

    [File Name] : File - the actual File
    [File Name]ContentType : String - the content type of the file
    [File Name]FileName : String - the actual name of the file uploaded (not the HTML name)
    

    For detail refer to the multiple file upload page of Struts2

    Multiple File Upload

    Showing user an option to add more file on the JSP page is matter of how to want to display that.All you need to take care when you show him file-upload box name should be same so that Struts2 param interceptor can set them as a list in your Action class

    0 讨论(0)
  • 2020-12-20 03:07

    I implemented this by using two struts2 plugin which are, Struts2 JQuery Plugin and Struts2 Scope Plugin

    The answer is as following:

    include those dependencies in your maven POM file:

            <dependency>
                <groupId>com.jgeppert.struts2.jquery</groupId>
                <artifactId>struts2-jquery</artifactId>
                <version>3.2.0</version>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>com.googlecode.struts2scopeplugin</groupId>
                <artifactId>struts2-scope-plugin</artifactId>
                <version>1.0.4</version>
            </dependency>
    

    and we create a class to prepare the form as follows:

     public class PrepareUpload extends ActionSupport{
         @In (scope=ScopeType.CONVERSATION)
          @Out (scope=ScopeType.CONVERSATION)
          private List<File> fileUploadi = new ArrayList<File>();
    
          @In (scope=ScopeType.CONVERSATION)
          @Out (scope=ScopeType.CONVERSATION)
            private List<String> fileUploadContentTypei = new ArrayList<String>();
    
          @In (scope=ScopeType.CONVERSATION)
          @Out (scope=ScopeType.CONVERSATION)
            private List<String> fileUploadFileNamei = new ArrayList<String>();
    
        @Action(value = "/prepareupload", results = {
                @Result(location = "upload.jsp", name = "success")
              })
              @End
        public String execute() throws Exception
    
          {
            return SUCCESS;
    
          }
    
        public List<File> getFileUploadi() {
            return fileUploadi;
        }
    
        public void setFileUploadi(List<File> fileUploadi) {
            this.fileUploadi = fileUploadi;
        }
    
        public List<String> getFileUploadContentTypei() {
            return fileUploadContentTypei;
        }
    
        public void setFileUploadContentTypei(List<String> fileUploadContentTypei) {
            this.fileUploadContentTypei = fileUploadContentTypei;
        }
    
        public List<String> getFileUploadFileNamei() {
            return fileUploadFileNamei;
        }
    
        public void setFileUploadFileNamei(List<String> fileUploadFileNamei) {
            this.fileUploadFileNamei = fileUploadFileNamei;
        }
    
    }
    

    and a class for the add to list and to handle ajax call as follows:

    public class UploadAction extends ActionSupport {
    
      private static final long serialVersionUID = 7968544374444173511L;
      private static final Log  log              = LogFactory.getLog(UploadAction.class);
    
    
      private String            echo;
      @In (scope=ScopeType.CONVERSATION)
      @Out (scope=ScopeType.CONVERSATION)
      private List<File> fileUploadi = new ArrayList<File>();
      private List<File> fileUpload = new ArrayList<File>();
      @In (scope=ScopeType.CONVERSATION)
      @Out (scope=ScopeType.CONVERSATION)
        private List<String> fileUploadContentTypei = new ArrayList<String>();
      private List<String> fileUploadContentType = new ArrayList<String>();
      @In (scope=ScopeType.CONVERSATION)
      @Out (scope=ScopeType.CONVERSATION)
        private List<String> fileUploadFileNamei = new ArrayList<String>();
      private List<String> fileUploadFileName = new ArrayList<String>();
      @Action(value = "/uploada", results = {
        @Result(location = "simpleecho.jsp", name = "success")
      })
      @Begin
      public String execute() throws Exception
      {
    
    fileUploadi.addAll( fileUpload);
    fileUploadFileNamei.addAll(fileUploadFileName);
    fileUploadContentTypei.addAll(fileUploadContentType);
       for (int i = 0;  i < fileUploadFileNamei.size();  i++)
       {
           if (echo == null)
               echo =   fileUploadFileNamei.get(i);
           else
          echo +=  "<br>" + fileUploadFileNamei.get(i);
        log.info(echo);
       }
        return SUCCESS;
      }
    
      public String getEcho()
      {
        return echo;
      }
    
    
      public List<File> getFileUpload() {
            return fileUpload;
        }
    
        public void setFileUpload(List<File> fileUpload) {
            this.fileUpload = fileUpload;
        }
    
        public List<String> getFileUploadContentType() {
            return fileUploadContentType;
        }
    
        public void setFileUploadContentType(List<String> fileUploadContentType) {
            this.fileUploadContentType = fileUploadContentType;
        }
    
        public List<String> getFileUploadFileName() {
            return fileUploadFileName;
        }
    
        public void setFileUploadFileName(List<String> fileUploadFileName) {
            this.fileUploadFileName = fileUploadFileName;
        }
    
        public List<File> getFileUploadi() {
            return fileUploadi;
        }
    
        public void setFileUploadi(List<File> fileUploadi) {
            this.fileUploadi = fileUploadi;
        }
    
        public List<String> getFileUploadContentTypei() {
            return fileUploadContentTypei;
        }
    
        public void setFileUploadContentTypei(List<String> fileUploadContentTypei) {
            this.fileUploadContentTypei = fileUploadContentTypei;
        }
    
        public List<String> getFileUploadFileNamei() {
            return fileUploadFileNamei;
        }
    
        public void setFileUploadFileNamei(List<String> fileUploadFileNamei) {
            this.fileUploadFileNamei = fileUploadFileNamei;
        }
    
    
    
    
        }
    

    and finally the jsp should be something like:

    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
    
    <html>
    <head>
    <sj:head jqueryui="true" defaultLoadingText="Please wait ..."/>
    <s:head />
    </head>
    
    <body>
    <h1>File Upload:</h1>
    
    <s:form action="uploada" 
    method="POST" enctype="multipart/form-data" theme="xhtml" >
    
    <s:file label="File:" name="fileUpload" size="40" />
    
     <div id="result" >Add Files Below:</div>
    <sj:submit 
                targets="result" 
                button="true" 
                validate="true" 
                value="Add File" 
                indicator="indicator"
                parentTheme="xhtml"
                />
    <sj:submit 
                button="true" 
                validate="true" 
                value="Submit" 
                indicator="indicator"
                parentTheme="xhtml"
                />
    
    </s:form>
    
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题