问题描述
我想捕获此异常,而不是简单地将500退还给最终用户,这是一个糟糕的体验,至少在我的应用程序中.目的是使用户返回到表单页面,并提供一些反馈,以便他们再次尝试.
I would like to catch this exception rather than simply returning a 500 to the end users which is a poor experience, at least in my application.The intention would be to return the user back to the form page with some feedback for them to try again.
当前的经验是将用户退回500,并将以下内容打印到日志中;
The current experience is to throw the user back a 500 and the following is printed to the logs;
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (157552) exceeds the configured maximum (1024)
推荐答案
为此起点而对@ james-kleeh进行信用;
Crediting @james-kleeh for this head start;
但是当我扩展StandardServletMultipartResolver
实现时,我只能在Grails 4.0.0.M2上使用它,这是默认设置.然后,可以继续从config(yaml)解析maxFileSize限制.
But I could only get this working on Grails 4.0.0.M2 when I extend the StandardServletMultipartResolver
implementation which is what is used as default. Then the maxFileSize limits continue to be resolved from config (yaml).
public class MyMultipartResolver extends StandardServletMultipartResolver {
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<String, MultipartFile>(), new LinkedHashMap<String, String[]>(), new LinkedHashMap<String, String>());
}
}
}
在resources.groovy中具有以下内容;
With the following in resources.groovy;
// catch exception when max file size is exceeded
multipartResolver(MyMultipartResolver)
您随后需要检查控制器中的 FILE_SIZE_EXCEEDED_ERROR 属性并进行相应的处理.
You need to subsequently check for the FILE_SIZE_EXCEEDED_ERROR attribute in the controller and handle accordingly.
这篇关于Grails4-在上传文件时超过文件maxFileSize限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!