1、建一个上传页面multiUpload.jsp
<body>
<h4>this is the fileupload2.jsp for many file</h4>
<form action="fileupload" method="post" enctype="multipart/form-data">
username : <input type="text" name="username" ><br>
file1 : <input type="file" name="file"><br>
file2 : <input type="file" name="file"><br>
file3 : <input type="file" name="file"><br>
<input type="submit" value="submit"><br>
</form>
</body>
2、写对应的action处理类:
public class UpLoad2Action extends ActionSupport {
private String username;
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception {
for (int i = 0; i < file.size(); i++) {
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath("/upload");
File dest = new File(root, fileFileName.get(i));
OutputStream os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is.close();
os.close();
}
return SUCCESS;
}
}
3、配置struts.xml文件:
<action name="fileupload" class="com.strong.action.UpLoad2Action">
<result name="success">/present2.jsp</result>
</action>
4、显示页面:
<body>
<h4>this is the fileUploadResult2.jsp</h4>
username : <s:property value="username"/><br>
<!-- 迭代去取上传文件的名字 ,-->
<s:iterator value="fileFileName" id="f">
fileName: <s:property value="#f"/><br>
</s:iterator>
</body>