Reputation: 19356
I have an application that let you upload some photos. I have a jsp page with a form to select what photo you want to upload:
subir-foto.jsp
...
<s:form action="SubirFoto" method="post" enctype="multipart/form-data" theme="bootstrap">
<s:file name="foto" label="Foto" />
<s:submit value="Upload" align="center" />
</s:form>
...
I want that with this form, the user can only upload certain type of files (like jpg, gif ...) and don't let the user to upload photos with a size bigger than 2 MB.
struts.xml
<action
name="SubirFoto"
class="impl.redex.presentation.profile.upload.SubirFotosAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes">
image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="dentroStack"></interceptor-ref>
<result name="success">/WEB-INF/jsps/foto/foto-subida.jsp</result>
<result name="input">/WEB-INF/jsps/foto/subir-foto.jsp</result>
</action>
The file uploading works perfectly except one thing. I can upload any photo if it satisfies the requirements asked. If I try to upload a file that is not a photo (for example, a plain text file), the application puts me the error that I have defined in global.properties
in the input of the form of the jsp.
global.properties
struts.messages.error.uploading - No se ha podido subir el fichero
struts.messages.error.file.too.large - El fichero es demasiado grande. El tamaño máximo es de 2MB.
struts.messages.error.content.type.not.allowed - Tipo de fichero no aceptado. Sólo es válido si la foto \
está en formato jpg/jpeg, gif o png.
The problem comes if I try to upload an image bigger than 2 MB, the application redirects me to subir-foto.jsp but it doesn't put any error in the jsp page.
I have experimented a bit and if I put <s:actionerror />
in subir-foto.jsp an when I upload the big photo it appear this:
the request was rejected because its size (17224595) exceeds the configured maximum (2097152)
As you can see, this is not the error that I have defined in global.properties
.
Why are not this two different errors field errors? Is this a problem with bootstrap plugin? Or is it a bug of Struts2? Am I doing something wrong? Any help with be appreciated, thanks.
Upvotes: 1
Views: 2583
Reputation: 500
Firstly, This error comes into picture if you are trying to upload a file more than the file size you specified for struts.multipart.maxSize
There's a pretty easy solution: In your struts.xml, increase the file size value of struts.multipart.maxSize,
<constant name="struts.multipart.maxSize" value="50777216000" />
<param name="maximumSize">15728640</param>
Keep param name="maximumSize" value less than struts.multipart.maxSize value,
As in above case, you will get your customised error defined in global.properties unless you exceed struts.multipart.maxSize limit..so try to keep struts.multipart.maxSize value to some high range.
Upvotes: 0
Reputation: 19356
I search more in Google and I found a blog that explain the error (the blog entry is from 2008 and I'm using Struts 2.3.4):
http://blog.bielu.com/2008/09/solution-to-struts2-upload-file-error.html
I summarize how I solve my problem. The problem was that the message the request was rejected because its size ...
is hardcoded in one of the Struts2 classes.
There are at least three solutions to this problem (all of them are pain in the ass): one is to reimplement
org.apache.struts2.dispatcher.multipart.MultiPartRequest
class; second one is to reimplementorg.apache.struts2.interceptor.FileUploadInterceptor
class.
The third one is put a validate
method in the FileUpload class.
@Override
public void validate() {
boolean error = false;
Collection<?> tmp = getActionErrors();
for (Object o : tmp) {
if (o.toString().contains(
"the request was rejected because its size")) {
if (!error) {
addFieldError("foto",
"El tamaño de la foto es muy grande (Máximo: 2MB)");
error = true;
}
}
}
}
Anyway, I still can't understand why the message that I defined wasn't working because in all sites that I search told me to overwrite the struts.messages.error.file.too.large
or why were both errors different type of errors (field and action errors).
Thanks to everybody for your time.
Upvotes: 5