Struts2文件上传超出配置大小的解决办法
用Struts2做一个文件上传来着,本来是想写个验证方法来限制文件大小的,结果发现根本走不到我的验证方法就开始报异常了:
THE REQUEST WAS REJECTED BECAUSE ITS SIZE EXCEEDS THE CONFIGURED MAXIMUM
,因为拦截器先把它给截下来了,根本不给我机会啊,咋个弄呢?Google了一下发现,Struts2上床文件先要通过commons-fileupload的检查,通过了才会交给struts2的fileUpload拦截器,一开始我把fileUpload的参数设置的很大都不行,因为没覆盖文件的maxSize,不扯了,下面是解决办法:
首先在配置文件中添加两个常量:
1 <constant name="struts.multipart.maxSize" value="107374182400" /> //设大点,保证不会使commons-fileupload产生异常,它通过了后面才能交给Struts2的l // fileUpoad的拦截器
2 <constant name="struts.custom.i18n.resources" value="struts-messages" />
接着创建struts-messages.properties.
1 struts.messages.error.content.type.not.allowed=Please upload a valid file. The allowed file types are; jpg, gif. bmp, doc, xls and pdf 2 struts.messages.error.file.too.large=File is too large. Max allowed size is 2 MB.
然后再struts.xml中添加拦截器:
1 <interceptor-ref name=”fileUpload”> 2 <param name=”maximumSize”>1572864</param> 3 <param name=”allowedTypes”> 4 image/gif,image/jpeg,image/png,image/bmp,application/msword,text/plain, 5 6 application/pdf,application/ms-excel,application/vnd.ms-excel,image/bitmap 7 </param> 8 </interceptor-ref>