I'm having two problems when trying to configure the Struts 2 File Upload Interceptor in my application. I want to change the parameter maximumSize
(the default value is 2 MB, I need it to be 5 MB) and the message resource struts.messages.error.file.too.large
(the app locale is pt_BR, so the message is in portuguese, not english).
The app current configuration follows:
struts.properties
struts.locale=pt_BR
struts.custom.i18n.resources=MessageResources
struts.xml
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="login" class="br.com.probank.interceptor.LoginInterceptor"/>
<interceptor-stack name="defaultLoginStack">
<interceptor-ref name="login" />
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultLoginStack" />
...
</package>
...
<package name="proposta" namespace="/proposta" extends="default">
<action name="salvarAnexoProposta" method="salvarAnexoProposta" class="br.com.probank.action.AnexoPropostaAction">
<interceptor-ref name="defaultLoginStack">
<param name="fileUpload.maximumSize">5242880</param>
</interceptor-ref>
<result name="success">/jsp/listagemAnexosPropostaForm.jsp</result>
<result name="input">/jsp/crudAnexoPropostaForm.jsp</result>
<result name="error">/jsp/error.jsp</result>
<result name="redirect" type="redirect">${redirectLink}</result>
</action>
</package>
MessageResources.properties
...
struts.messages.error.file.too.large=O tamanho do arquivo...
There is nothing special about my Action implementation and my JSP code. They follow the example found http://struts.apache.org/2.1.6/docs/file-upload-interceptor.html. When I try to upload a file with more than 5 MB the app shows the message "the request was rejected because its size (6229458) exceeds the configured maximum (2097152)" - the default File Upload message with the default maximumSize value.
I try to put the message resource struts.messages.error.file.too.large
in a struts-messages.properties but the message didn't change after that. What is the proper way to configure the File Upload Interceptor? I'm using Struts 2 2.1.7. Thanks in advance.
The solution provided by you isn't entirely correct in the sense that If I want strict upload limits along with i18n, this won't work. I've also created an issue with strut2 for this. Please look at the following link https://issues.apache.org/jira/browse/WW-3177. It's due to be fixed in struts2.1.9 and is already assigned to a struts team member.
In between, I'm using a hack. I browsed the struts2 source code and found the code for FileUploadInterceptor. Using that code, I created my own. Here's the code below. You can find details of problem at the link above. Hope this helps.
Try this in your
struts.xml
, wherexxxxxxxx
is the limit:Finally solved the entire puzzle!
struts.xml
andMessageResource.properties
were correctly configured. The problem wasstruts.multipart.maxSize
value. This value have to be bigger than the desired upload limit (5242880 in my app), so I set it as 10000000. Ifstruts.multipart.maxSize
value is equal or less thenfileUpload.maximumSize
the library used by Struts 2 to do the upload stops the upload process (and writes the error message) before the file upload interceptor has a chance to do its job.First use validate method in your action file........
For complete code,please visit http://knowledge-serve.blogspot.com/2011/10/upload-file-using-in-struts-2.html