javaEE Struts2,檔案上傳,Action中接收檔案型別引數
阿新 • • 發佈:2019-02-04
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; } }