天天看點

檔案上傳時使用攔截器限制檔案大小類型無效的問題

使用注解配置struts2action用于檔案上傳

@Controller()

@Namespace("/clientAccept")

public class ClientAcceptAction{

//get set方法一定要加

private File luploadDoc;

 private String luploadDocFileName;

// 上傳資質資料

 @Action(value = "toUploadLicence", results = { @org.apache.struts2.convention.annotation.Result(name = "input", type = "dispatcher", location = "/client/fileerror.jsp") }, interceptorRefs = {

   @InterceptorRef(value = "fileUpload", params = {

     "maximumSize",

     "4194304",

     "allowedTypes",

     "image/bmp,image/PNG,image/gif,image/pjpeg,image/JPEG,image/JPG,image/jpg,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" }),

   @InterceptorRef(value = "defaultStack") })

 // defaultStack

 public String toUploadLicence() throws Exception {

}

}

在使用攔截器時 預設的比如自動填充 檔案上傳等攔截器全部都不會初始而使用 必須顯示的添加 @InterceptorRef(value = "defaultStack") 預設的攔截器

同時使用 fileUpload 的配置 覆寫檔案上傳的攔截器 這樣不影響全局的配置 隻應用到該action中

比如在struts.properties配置了檔案大小為60M 而該action配置的是4M  然而運作發現該攔截器 沒起到作用 不知道為何

找了多方面資料 才知道 使用注解時使用到了 ActionSupport 類中的某些東西

使用注解使用攔截器時必須要繼承ActionSupport   才能生效 修改為:

@Controller()

@Namespace("/clientAccept")

public class ClientAcceptAction extends ActionSupport {

//get set方法一定要加

private File luploadDoc;

 private String luploadDocFileName;

// 上傳資質資料

 @Action(value = "toUploadLicence", results = { @org.apache.struts2.convention.annotation.Result(name = "input", type = "dispatcher", location = "/client/fileerror.jsp") }, interceptorRefs = {

   @InterceptorRef(value = "fileUpload", params = {

     "maximumSize",

     "4194304",

     "allowedTypes",

     "image/bmp,image/PNG,image/gif,image/pjpeg,image/JPEG,image/JPG,image/jpg,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" }),

   @InterceptorRef(value = "defaultStack") })

 // defaultStack

 public String toUploadLicence() throws Exception {

}

}

這樣就可以攔截到了