gpt4 book ai didi

java - 如何在Grails 3中捕获 “org.springframework.web.multipart.MultipartException”

转载 作者:行者123 更新时间:2023-12-02 15:53:14 26 4
gpt4 key购买 nike

我很难捕捉到该错误:org.springframework.web.multipart.MultipartException我有以下代码:

final class UploadTextCommand  {
MultipartFile contents
static constraints = {
}
}
另外,要处理文件的发布,我需要执行以下操作:
def upload = { UploadTextCommand -> 
...code...
}
我在“application.yml”文件中设置了以下设置以强制执行错误:
controllers:
upload:
maxFileSize: 100
maxRequestSize: 100
因此,我可以可靠地重新创建异常,但是在浏览器显示出可怕的 View 之前,我无法捕获到该异常:
HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException
请告诉我如何在Grails中捕获此错误。仅在上载操作中使用try / catch会失败。
根据评论者的要求添加了堆栈跟踪:
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)

最佳答案

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

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



并在您的代码中:
controllers:
upload:
maxFileSize: 100
maxRequestSize: 100

您应该超过 maxFileSize,但如果要处理异常 check this solution。恕我直言,更清洁的解决方案将是您自己的 before filterinterceptor的实现。

关于java - 如何在Grails 3中捕获 “org.springframework.web.multipart.MultipartException”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com