2012-07-03 28 views
1

我有一个应用程序,可以让你上传一些照片。我有一个jsp页面与表单选择要上传的照片是什么:为什么jF中没有显示(正确)错误“file.too.large”?

苏比尔-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上传图片越大,应用重定向我苏比尔-foto.jsp,但它不把任何错误在jsp页面中。

我已经尝试了一下,如果我把<s:actionerror />苏比尔-foto.jsp的时候我上传的照片大就出现这样的:

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

正如你所看到的,这不是错误,我已在global.properties中定义。

为什么这两个不同的错误现场错误?这是引导插件的问题吗?或者它是Struts2的错误?难道我做错了什么?任何帮助表示赞赏,谢谢。

+0

是否将global.properties注册为自定义i18n资源?例如,在你的struts.xml(或struts.properties)中:''? –

+0

是的,我有我的struts.xml行'<常数 \t \t NAME = “struts.custom.i18n.resources” \t \t值= “全球”/>' – Pigueiras

回答

5

我更多的搜索在谷歌,我发现,解释该错误的博客(博客条目是从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为什么都是错误的不同类型的错误(场动作错误)。

感谢大家为你的时间。

+0

+1 - 尼斯找到。没有一种解决方案特别好。将异常消息作为行为错误传递的方法将导致国际化问题。 –

+0

https://issues.apache.org/jira/browse/WW-3177 –

0

首先,这个错误进入画面,如果你想上传的文件超过您对文件上传用

有一个非常简单的解决方案中指定的文件大小: 在你的struts.xml,增加文件上传用的文件大小值,

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

保持PARAM NAME =“MAXIMUMSIZE”值小于文件上传用价值,
如在上述情况下,你会得到你自定义的错误defin编辑在global.properties,除非你超过struts.multipart.maxSize限制..所以尝试保持struts.multipart.maxSize值到一定的高范围。

相关问题