使用list集合实现多文件上传
通过配置下面这句话可以改变上传文件大小的总大小上限
<constant name="struts.multipart.maxSize" value="2097152000"></constant>
JSP数据提交页面如下所示:
<body>
<form action="uploadlist.action" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<br />
<input type="file" name="file">
<br />
<input type="file" name="file">
<br />
<input type="submit" value="上传">
</form>
</body>
Action:
package com.mth.upload;
import java.io.File;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadList extends ActionSupport {
private List<File> file;
// FileName ContentType 是固定格式
private List<String> fileFileName;
private List<String> fileContentType;
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 {
String path = ServletActionContext.getServletContext().getRealPath(
"/upload");
File file1 = null;
for (int i = 0; i < file.size(); i++) {
// 在path的路径下面 创建一个叫做fileFileName.get(i)的文件
file1 = new File(path, fileFileName.get(i));
// 源文件 目标文件
FileUtils.copyFile(file.get(i), file1);
}
return SUCCESS;
}
}
配置文件的配置方式:
<package name="upload" extends="struts-default" namespace="/">
<action name="uploadlist" class="com.mth.upload.UploadList">
<result>/UpSuccess.jsp</result>
<result name="input">/ErrorUpload.jsp</result>
</action>
</package>