struts2文件上传Action
package cn.itcast.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class HelloWorldAction {
private File image;
private String imageFileName;
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String addUI(){
return "success";
}
public String execute() throws Exception{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if(image!=null){
File savefile = new File(new File(realpath), imageFileName);
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
}