【发布时间】:2012-07-03 11:32:16
【问题描述】:
我有一个应用程序可以让你上传一些照片。我有一个带有表单的 jsp 页面,用于选择您要上传的照片:
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>
...
我希望通过这种形式,用户只能上传某些类型的文件(如 jpg、gif ...),并且不要让用户上传大小超过 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>
文件上传工作完美,除了一件事。如果满足要求,我可以上传任何照片。如果我尝试上传不是照片的文件(例如,纯文本文件),应用程序会将我在 global.properties 中定义的错误以 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.
如果我尝试上传大于 2 MB 的图像,应用程序会将我重定向到 subir-foto.jsp,但它不会在 jsp 页面中出现任何错误。 p>
我进行了一些实验,如果我将<s:actionerror /> 放在 subir-foto.jsp 中,当我上传大照片时,它会出现:
the request was rejected because its size (17224595) exceeds the configured maximum (2097152)
如您所见,这不是我在global.properties 中定义的错误。
为什么不是这两个不同的error字段错误?这是引导插件的问题吗?或者它是Struts2的一个错误?难道我做错了什么?任何帮助都将不胜感激,谢谢。
【问题讨论】:
-
global.properties 是否注册为自定义 i18n 资源?例如,这是否在您的 struts.xml(或 struts.properties)中:
<constant name="struts.custom.i18n.resources" value="global"/>? -
是的,我的 struts.xml 中有
<constant name="struts.custom.i18n.resources" value="global" />行
标签: java file-upload struts2 validation