java - 为什么错误 "file.too.large"没有在 jsp 中显示(正确)?

标签 java file-upload struts2 validation

我有一个应用程序可以让你上传一些照片。我有一个带有表单的 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 页面中放置任何错误。

我做了一些实验,如果我把 <s:actionerror />在 subir-foto.jsp 中,当我上传大照片时,它显示为:

the request was rejected because its size (17224595) exceeds the configured maximum (2097152)

如您所见,这不是我在global.properties 中定义的错误.

为什么不是这两个不同的错误字段错误呢?这是 Bootstrap 插件的问题吗?还是Struts2的bug?难道我做错了什么?感谢任何帮助,谢谢。

最佳答案

我在 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 ... is hardcoded in one of 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 reimplement org.apache.struts2.interceptor.FileUploadInterceptor class.

第三个是在 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 或者为什么这两个错误都是不同类型的错误(字段和操作错误)。

感谢大家的宝贵时间。

关于java - 为什么错误 "file.too.large"没有在 jsp 中显示(正确)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11310016/

相关文章:

java - 空数组到空列表

java - 如何在多个 Activity 中使用 ViewModel 和 LiveData 观察者?

java - 如何将方法作为参数传递

php - 动态下拉文件上传的文件上传进度

java - struts2约定插件在EAR中不起作用,但在WAR中工作正常

java - Struts 2 ajax 验证找不到拦截器类 jsonValidationWorkflowStack

java - 如何从 Android 的 ListView 中删除项目?有像iOS那样的内置控件吗?

ruby-on-rails - 使用 rake 脚本、carrierwave 和 fog 的 S3 上传文件不起作用

jquery-ui - 有没有办法判断最后一个ajax调用是否完成?

java - 防止多次提交 Struts2 : Not Showing Success Message