如何捕获"org.springframework.web.multipart.MultipartException"在Grails 3中 [英] How to catch "org.springframework.web.multipart.MultipartException" in Grails 3

查看:130
本文介绍了如何捕获"org.springframework.web.multipart.MultipartException"在Grails 3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难捕捉到错误:org.springframework.web.multipart.MultipartException

我有以下代码:

final class UploadTextCommand  {
   MultipartFile contents
   static constraints = {
   }
}

另外要处理文件的发布,我需要执行以下操作:

Additionally to handle the post of the file I have this action:

def upload = { UploadTextCommand -> 
   ...code...
}

我已经在"application.yml"目录中获得了这些设置.文件以强制错误:

I've got these settings in the "application.yml" file to force the error:

controllers:
    upload:
        maxFileSize: 100
        maxRequestSize: 100

因此,我可以可靠地重新创建异常,但是在浏览器显示出可怕的视图之前,我无法捕获该异常:

So I can reliably recreate the exception, but I can't catch it before the browser shows a horrible view:

HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException

请告诉我如何在Grails中捕获此错误.仅在上传操作中使用try/catch会失败.

Please tell me how to catch this error in Grails. Just using a try/catch in the upload action fails miserably.

应评论者的要求添加了堆栈跟踪:

Added stack trace on request of commenter:

javax.servlet.ServletException: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (318635) exceeds the configured maximum (100000)
grails.plugin.cache.web.filter.AbstractFilter.logThrowable(AbstractFilter.java:116)
grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:70)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:73)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113)
org.springframework.boot.context.web.ErrorPageFilter.forwardToErrorPage(ErrorPageFilter.java:188)
org.springframework.boot.context.web.ErrorPageFilter.handleException(ErrorPageFilter.java:171)
org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:135)
org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:61)
org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:95)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113)

推荐答案

您可以在堆栈跟踪中找到:

You can find in a stacktrace:

该请求被拒绝,因为其大小(318635)超出了 配置的最大值(100000)

the request was rejected because its size (318635) exceeds the configured maximum (100000)

并在您的代码中:

controllers:
    upload:
        maxFileSize: 100
        maxRequestSize: 100

您应该超过maxFileSize,但是如果您要处理异常,请检查此解决方案.恕我直言,更清洁的解决方案将是您自己的before filterinterceptor的实现.

you should exceed maxFileSize, but if you want to handle exception check this solution. IMHO cleaner solution would be implementation of your own before filter or interceptor.

这篇关于如何捕获"org.springframework.web.multipart.MultipartException"在Grails 3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆