【问题标题】:Why does the error "file.too.large" is not being showed (correctly) in the jsp?为什么在 jsp 中没有(正确)显示错误“file.too.large”?
【发布时间】: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>

我进行了一些实验,如果我将&lt;s:actionerror /&gt; 放在 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)中:&lt;constant name="struts.custom.i18n.resources" value="global"/&gt;?
  • 是的,我的 struts.xml 中有 &lt;constant name="struts.custom.i18n.resources" value="global" /&gt;

标签: java file-upload struts2 validation


【解决方案1】:

我在 Google 中搜索了更多内容,发现了一个解释错误的博客(该博客条目来自 2008 年,我使用的是 Struts 2.3.4):

http://blog.bielu.com/2008/09/solution-to-struts2-upload-file-error.html

我总结了我如何解决我的问题。问题是消息the request was rejected because its size ... 被硬编码在一个Struts2 类中。

这个问题至少有三个解决方案(都是头疼的):一个是重新实现org.apache.struts2.dispatcher.multipart.MultiPartRequest类;第二个是重新实现org.apache.struts2.interceptor.FileUploadInterceptor类。

第三个是在 FileUpload 类中放一个validate 方法。

@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;
            }
        }
    }

}

无论如何,我仍然无法理解为什么我定义的消息不起作用,因为在我搜索的所有网站中都告诉我覆盖 struts.messages.error.file.too.large 或者为什么两个错误都是不同类型的错误(字段和操作错误)。

感谢大家的宝贵时间。

【讨论】:

  • +1 不错的发现。没有一个解决方案特别好。将异常消息作为操作错误传递的方法将导致国际化问题。
【解决方案2】:

首先,如果您尝试上传的文件大于您为 struts.multipart.maxSize 指定的文件大小,则会出现此错误

有一个非常简单的解决方案: 在你的struts.xml中,增加struts.multipart.maxSize的文件大小值,

  <constant name="struts.multipart.maxSize" value="50777216000" />
  <param name="maximumSize">15728640</param>

保持param name="maximumSize"值小于 struts.multipart.maxSize值,
与上述情况一样,除非您超过 struts.multipart.maxSize 限制,否则您将在 global.properties 中定义自定义错误。所以请尝试将 struts.multipart.maxSize 值保持在某个较高的范围内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2020-02-24
    相关资源
    最近更新 更多