JSP不允许选择多个文件上传

时间:2015-09-26 09:25:19

标签: html jsp struts2

我是Struts2的新手。我已经编写了一个JSP页面,可以在单个上传选项中接受多个文件,但它不会在索引页面中接受多个文件。

请在下面找到index.jsp文件。

<s:form action="upload" method="post" enctype="multipart/form-data">
<label for="myFile">Upload your file</label>
 <s:file name="myFile" multiple="multiple" />
  <s:submit value="Upload files" />
  </s:form>

请帮我解决这个问题。它允许单个文件选择。

1 个答案:

答案 0 :(得分:0)

从这里开始:Need to upload multiple files at once

  

JSP

<%@page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Multiple File Upload Example</title>
</head>
<body>
    <s:form action="upload" enctype="multipart/form-data" >
        <s:file name="files" multiple="multiple" />
        <s:submit value="Upload files" />
    </s:form>
</body>
</html>
     

ACTION

     

公共类上传扩展了ActionSupport {

private List<File> files;
private List<String> filesContentType;
private List<String> filesFileName;

/* GETTERS AND SETTERS */           

public String execute() throws Exception{
    System.out.print("\n\n---------------------------------------");
    int i=0;
    for (File file : files){
        System.out.print("\nFile ["+i+"] ");
        System.out.print("; name:"         + filesFileName.get(i));
        System.out.print("; contentType: " + filesContentType.get(i));
        System.out.print("; length: "      + file.length());
        i++;
    }
    System.out.println("\n---------------------------------------\n");
    return SUCCESS;
}

}
     

Struts.xml - 最大多部分大小:

<constant name="struts.multipart.maxSize" value="20000000" /> 
     

拦截

<interceptor-ref name="fileUpload">
  <param name="maximumSize">10485760</param>
</interceptor-ref>