import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpload extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private File[] file;
private String[] fileContentType;
private String[] file1FileName;
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileContentType() {
return fileContentType;
}
public void setFileContentType(String[] fileContentType) {
this.fileContentType = fileContentType;
}
public String[] getFileFileName() {
return file1FileName;
}
public void setFileFileName(String[] fileFileName) {
this.file1FileName = fileFileName;
}
@Override
public String execute() throws Exception {
for(int i = 0; i < file.length; i++){
File destFile = new File(ServletActionContext.getServletContext().getRealPath("/upload"),file1FileName[i]);
FileUtils.copyFile(file[i], destFile);
}
return SUCCESS;
}
}
多文件上传
<form action="fileupload" method="post" enctype="multipart/form-data">
文件<input type="file" name="file">
文件<input type="file" name="file">
文件<input type="file" name="file">
<input type="submit" name="提交">
</form>
对应struts.xml
<action name="fileupload" class="com.java111.FileUpload">
<result name="success">main.jsp</result>
<interceptor-ref name="fileUpload">
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>