Grails 2.4限制/最大上传大小

时间:2015-03-17 16:04:08

标签: grails file-upload

关于https://brainflush.wordpress.com/2008/12/04/how-to-gracefully-recover-from-file-upload-errors-in-grails/我无法在Grails 2.4上使用它。

我已将此添加到我的Grails应用中:

resources.groovy

beans = {
    multipartResolver(CustomMultipartResolver) {
        maxUploadSize = 2000000
    }
}

添加到BootStrap.groovy

public class CustomMultipartResolver extends CommonsMultipartResolver {

   static final String FILE_SIZE_EXCEEDED_ERROR = "fileSizeExceeded"

   public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) {

      try {
         return super.resolveMultipart(request)
      } catch (MaxUploadSizeExceededException e) {
         request.setAttribute(FILE_SIZE_EXCEEDED_ERROR, true)
         return new DefaultMultipartHttpServletRequest(request, new LinkedMultiValueMap(), [:])
      } catch (SizeLimitExceededException e) {
         request.setAttribute(FILE_SIZE_EXCEEDED_ERROR, true)
         return new DefaultMultipartHttpServletRequest(request, new LinkedMultiValueMap(), [:])
      }
   }
}

在控制器中:

if (request.getAttribute(CustomMultipartResolver.FILE_SIZE_EXCEEDED_ERROR)) {
   ModActionFile.errors.reject("MyDomain.fileSizeTooLarge")
   redirect(controller:'MyDomain', action:'create',params:params)
} else {...

我收到此错误

Error |
2015-03-17 16:08:57,794 [http-bio-8080-exec-8] ERROR [/tool].[default]  - Servlet.service() for servlet [default] in context with path [/tool] threw exception
Message: Could not find matching constructor for: org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest(org.apache.catalina.connector.RequestFacade, org.springframework.util.LinkedMultiValueMap, java.util.LinkedHashMap)
    Line | Method
->>   43 | resolveMultipart in CustomMultipartResolver$$EP7CaraR
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1145 | runWorker        in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run              in java.lang.Thread

1 个答案:

答案 0 :(得分:2)

2008很久以前。

错误信息非常清楚 - 您要么将错误的类型传递给构造函数,要么传递错误的参数数量。

Grails 2.4.4使用Spring 4.0.7,该类有两个构造函数:

public DefaultMultipartHttpServletRequest(HttpServletRequest request, MultiValueMap<String, MultipartFile> mpFiles,
        Map<String, String[]> mpParams, Map<String, String> mpParamContentTypes)

public DefaultMultipartHttpServletRequest(HttpServletRequest request)

您可以看到here

这应该有效:

class CustomMultipartResolver extends CommonsMultipartResolver {

   static final String FILE_SIZE_EXCEEDED_ERROR = 'fileSizeExceeded'

   MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) {
      try {
         super.resolveMultipart request
      }
      catch (MaxUploadSizeExceededException e) {
         request.FILE_SIZE_EXCEEDED_ERROR = true
         new DefaultMultipartHttpServletRequest(request, new LinkedMultiValueMap(), [:], [:])
      }
      catch (SizeLimitExceededException e) {
         request.FILE_SIZE_EXCEEDED_ERROR = true
         new DefaultMultipartHttpServletRequest(request, new LinkedMultiValueMap(), [:], [:])
      }
   }
}