CustomerAction.java(Action对象,接收文件类型参数):
package cn.xxx.web.action;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.xxx.domain.Customer;
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer(); //Action接收参数的方式,模型驱动和属性驱动等方式可以混用(一起用)
//上传的文件会自动封装到File对象
//"photo"要与前台<input type="file" name="photo" />组件 name属性值相同
private File photo;
//上传文件的名称会自动封装到xxxFileName属性中
private String photoFileName;
//上传文件的MIME类型会自动封装到xxxContentType属性中
private String photoContentType;
@Override
public String execute() throws Exception {
if(photo!=null){
System.out.println("文件名称:"+photoFileName); //abc.jpg
System.out.println("文件MIME类型:"+photoContentType); //image/jpeg
//将上传的文件保存到指定位置
photo.renameTo(new File("E:/upload/xxx.jpg"));
}
// ...................
return "success";
}
@Override
public Customer getModel() {
return customer;
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
}