Struts2 文件上传 [英] Struts2 File Upload

查看:22
本文介绍了Struts2 文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以一种形式上传多个文件,并且文件数量因用户而异,例如.一个用户可以上传 2 个文件,另一个用户可以上传 12 个文件.我想知道如何在 jsp struts2 表单中创建循环或其他内容,这样当用户上传文件时,它会将其名称添加到列表中,并且他/她可以上传另一个文件并添加到列表中等等.

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 want to know how could I create a loop or something in the jsp struts2 form such that when a user uploads a file it adds its name to a list and he/she could upload another file and be added to the list and so on.

推荐答案

我使用两个struts2插件实现了这个,分别是Struts2 JQuery Plugin和Struts2 Scope Plugin

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

答案如下:

在您的 maven POM 文件中包含这些依赖项:

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;
    }

}

以及一个用于添加到列表和处理 ajax 调用的类,如下所示:

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;
    }




    }

最后jsp应该是这样的:

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>

这篇关于Struts2 文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆