I suggest you to use Struts Tags instead of plain HTML tags, and to extend ActionSupport (returning its Result constants instead of manually composing the result String, like "result").
That said, this is a tested and working example.
Note: It won't work on old versions of IE, but since you are using HTML5 in your own question, I bet you already know it and you are not targeting old IE.
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>
Note about the multiple="multiple"
part: even if in the official documentation, that attribute for the <s:file />
tag is not defined, since Struts 2.1 it is allowed because of
Dynamic Attributes Allowed:
true
this means that it will be drawn on the JSP as-is, without any interference by Struts. This way Struts doens't need to update its Tags each time HTML5 provides a new feature; you could put foo="bar"
too in a tag that allows Dynamic Attributes (<s:file />
, <s:textarea />
, etc), and you will find it in the HTML.
Action
public class Upload extends ActionSupport{
private List<File> files;
private List<String> filesContentType;
private List<String> filesFileName;
/* GETTERS AND SETTERS */
public String execute() throws Exception{
System.out.print("
---------------------------------------");
int i=0;
for (File file : files){
System.out.print("
File ["+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("
---------------------------------------
");
return SUCCESS;
}
}
Then you may want to set the maximum size of the Request, and the maximum size of each single file, like described here:
Struts.xml - Max multipart size:
<constant name="struts.multipart.maxSize" value="20000000" />
Struts.xml - Max size of a file (globally to a package, or locally to an Action)
<interceptor-ref name="fileUpload">
<param name="maximumSize">10485760</param>
</interceptor-ref>