1、单文件上传
private File file; //与表单中的name属性一致
private String file[color=red]FileName[/color]; //表单中的name属性+FileName
String realPath = ServletActionContext.getServletContext ().getRealPath("/image");
File file1 = new File(new File(realPath),fileFileName);
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
try {
[color=red]FileUtils.copyFile(file, file1);[/color] } catch (IOException e) {
e.printStackTrace();
}
}
2、多文件上传(单文件循环上传)
private File file; //与表单中的name属性一致
private String file[color=red]FileName[/color]; //表单中的name属性+FileName
String realPath = ServletActionContext.getServletContext ().getRealPath("/image");
File file1 = new File(new File(realPath),fileFileName);
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
try {
[color=red]FileUtils.copyFile(file, file1);[/color] } catch (IOException e) {
e.printStackTrace();
}
}
2、多文件上传(单文件循环上传)
package action;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class FileAction {
public String execute(){
ServletContext sc = ServletActionContext.getServletContext();
String realPath = sc.getRealPath("/image");
File realFile = new File(realPath);
if(!realFile.exists())
realFile.mkdir();
int i = 0 ;
for(File file1 : file){
try {
FileUtils.copyFile(file1, new File(realFile,fileFileName[i]));
} catch (IOException e) {
e.printStackTrace();
}
i++;
}
return "success";
}
private File[] file;
private String[] fileFileName;
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
}