簡體   English   中英

如何在 Grails 3 中捕獲“org.springframework.web.multipart.MultipartException”

[英]How to catch “org.springframework.web.multipart.MultipartException” in Grails 3

我很難捕捉到錯誤: org.springframework.web.multipart.MultipartException

我有以下代碼:

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

另外為了處理文件的發布,我有這個動作:

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

我在“application.yml”文件中有這些設置來強制錯誤:

controllers:
    upload:
        maxFileSize: 100
        maxRequestSize: 100

所以我可以可靠地重新創建異常,但在瀏覽器顯示可怕的視圖之前我無法捕獲它:

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)

您可以在堆棧跟蹤中找到:

請求被拒絕,因為它的大小 (318635) 超過了配置的最大值 (100000)

並在您的代碼中:

controllers:
    upload:
        maxFileSize: 100
        maxRequestSize: 100

你應該超過maxFileSize ,但如果你想處理異常檢查這個解決方案 恕我直言,更清潔的解決方案是before filterinterceptor before filter實現您自己的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM