struts2出现 the request was rejected because its size (XXXX) exceeds the configured maximum (XXXX)



struts2出现 the request was rejected because its size (XXXX) exceeds the configured maximum (XXXX)

在struts2中我们上传文件大于struts.multipart.maxSize设置的值时会抛出the request was rejected because its size (XXXX) exceeds the configured maximum (XXXX)异常,他是不能被国际化的,这信息对应用户来说是非常不友好的,那任何处理?

 

在struts2.2 中有两个地方设置上传文件大小:

一个是在拦截器fileUpload中设置

 

[c-sharp] view plaincopy

  1. <interceptor-ref name=”fileUpload”>
  2.   <param name=”maximumSize”>1000000</param>
  3. </interceptor-ref>

 

一个是在struts2 自带的文件default.properties中设置的(系统默认大小2M)

struts.multipart.maxSize=2097152

这个可以在struts.propertise 文件中修改

 

那这两个有什么区别呢?

Struts2框架底层默认用的是apache的commons-fileupload组件对上传文件进行接受处理。struts.multipart.maxSize设置的大小就是该处理时取用的值,在上传文件之前系统会去比较文件的大小是否超过了该值,如果超过将抛出上述异常,commons-fileupload组件是不支持国际化的,所以我们看到的异常都是默认的。

fileUpload拦截器只是当文件上传到服务器上之后,才进行的文件类型和大小判断。 如果上传的文件大小刚好这struts.multipart.maxSize与maximumSize 之间会抛出 key  struts.messages.error.file.too.large 对应的异常信息,这个才支持国际化。那不是把struts.multipart.maxSize设很大,不就解决问题了吗?非也! fileUpload拦截器只是当文件上传到服务器上之后,才进行的文件类型和大小判断,这样会造成系统产生多余没用的文件。

 

那该如何是好?

其实解决的办法有多种,我这里介绍种最简单的方法:    在struts2.2 org.apache.commons.fileupload.FileUploadBase.java 中我们看到


 

[c-sharp] view plaincopy

  1. /**
  2. * Creates a new instance.
  3. * @param ctx The request context.
  4. * @throws FileUploadException An error occurred while
  5. *   parsing the request.
  6. * @throws IOException An I/O error occurred.
  7. */
  8. FileItemIteratorImpl(RequestContext ctx)
  9.         throws FileUploadException, IOException {
  10.     if (ctx == null) {
  11.         throw new NullPointerException(“ctx parameter”);
  12.     }
  13.     String contentType = ctx.getContentType();
  14.     if ((null == contentType)
  15.             || (!contentType.toLowerCase().startsWith(MULTIPART))) {
  16.         throw new InvalidContentTypeException(
  17.                 “the request doesn’t contain a “
  18.                 + MULTIPART_FORM_DATA
  19.                 + ” or “
  20.                 + MULTIPART_MIXED
  21.                 + ” stream, content type header is “
  22.                 + contentType);
  23.     }
  24.     InputStream input = ctx.getInputStream();
  25.     if (sizeMax >= 0) {
  26.         int requestSize = ctx.getContentLength();
  27.         if (requestSize == -1) {
  28.             input = new LimitedInputStream(input, sizeMax) {
  29.                 protected void raiseError(long pSizeMax, long pCount)
  30.                         throws IOException {
  31.                     FileUploadException ex =
  32.                         new SizeLimitExceededException(
  33.                             “the request was rejected because”
  34.                             + ” its size (” + pCount
  35.                             + “) exceeds the configured maximum”
  36.                             + ” (” + pSizeMax + “)”,
  37.                             pCount, pSizeMax);
  38.                     throw new FileUploadIOException(ex);
  39.                 }
  40.             };
  41.         } else {
  42. ///问题就在这里////////////////////////////////////
  43.             if (sizeMax >= 0 && requestSize > sizeMax) {
  44.                 throw new SizeLimitExceededException(
  45.                         “the request was rejected because its size (“
  46.                         + requestSize
  47.                         + “) exceeds the configured maximum (“
  48.                         + sizeMax + “)”,
  49.                         requestSize, sizeMax);
  50.             }
  51.         }
  52.     }
  53.     String charEncoding = headerEncoding;
  54.     if (charEncoding == null) {
  55.         charEncoding = ctx.getCharacterEncoding();
  56.     }
  57.     boundary = getBoundary(contentType);
  58.     if (boundary == null) {
  59.         throw new FileUploadException(
  60.                 “the request was rejected because “
  61.                 + “no multipart boundary was found”);
  62.     }
  63.     notifier = new MultipartStream.ProgressNotifier(listener,
  64.             ctx.getContentLength());
  65.     multi = new MultipartStream(input, boundary, notifier);
  66.     multi.setHeaderEncoding(charEncoding);
  67.     skipPreamble = true;
  68.     findNextItem();
  69. }

 

实际上他是把该异常信息设置为Action级别的错误信息。

了解完这个就好办了,我们可以在action中直接重写ActionSupport的addActionError()方法,

 

 

[c-sharp] view plaincopy

  1. /**
  2. * 替换文件上传中出现的错误信息
  3. *引用 import java.util.regex.Matcher;
  4. *    import java.util.regex.Pattern;
  5. *
  6. * */
  7. @Override
  8. public void addActionError(String anErrorMessage) {
  9. //这里要先判断一下,是我们要替换的错误,才处理
  10.    if (anErrorMessage.startsWith(“the request was rejected because its size”)) {
  11.      Matcher m = Pattern.compile(“//d+”).matcher(anErrorMessage);
  12.     String s1 = “”;
  13.     if (m.find())   s1 = m.group();
  14.     String s2 = “”;
  15.     if (m.find())   s2 = m.group();
  16.     //偷梁换柱,将信息替换掉
  17.      super.addActionError(“你上传的文件大小(” + s1 + “)超过允许的大小(” + s2 + “)”);
  18.     //也可以改为在Field级别的错误
  19.     // super.addFieldError(“file”,”你上传的文件大小(” + s1 + “)超过允许的大小(” + s2 + “)”);
  20.   } else {//否则按原来的方法处理
  21.      super.addActionError(anErrorMessage);
  22. }
  23. }

 

这时这页面增加

 

[c-sharp] view plaincopy

  1. <s:fielderror/>

 

就可以了。

 

做到这里还有个问题,就是原来页面上输入的其他文本内容也都不见了,也就是说params注入失败。

这个是没办法的,因为这个异常是在文件上传之前捕获的,文件未上传,同时params也为注入,所以这时最好重定向到一个jsp文件,提示上传失败,然后重写填写相应信息。

 

本文转自:http://blog.csdn.net/dxswzj/article/details/6260950

http://blog.csdn.net/dongzhouzhou/article/details/8448959