现在的位置: 首页 > 综合 > 正文

文件上传时使用拦截器限制文件大小类型无效的问题

2013年02月26日 ⁄ 综合 ⁄ 共 1734字 ⁄ 字号 评论关闭

使用注解配置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 {

}

}

这样就可以拦截到了

抱歉!评论已关闭.