一、大家都知道用Struts2框架上传单个文件非常的简单,其实多文件上传也一样,只不过是更改一下表单和Action代码而已,基本配置不在展示。
关键就是在Action中,针对的File必须写成数组形式或者说是List形式也是可以的。下面直接看一下代码
package com.qianyan.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class FileUpload {
private File[] image; //获取上传文件
private String[] imageFileName; //获取上传文件名称
private String[] imageContentType; //获取上传文件类型
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
public String execute() throws Exception{
String path = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(path);
if(image != null){
File savedir = new File(path);
if(!savedir.exists()) savedir.mkdirs();
for(int i = 0; i < image.length; i++){
File saveFile = new File(savedir,imageFileName[i]);
FileUtils.copyFile(image[i], saveFile);
}
}
return "success";
}
}