将@james-kleeh 归功于这一领先优势;
StandardServletMultipartResolver
但是当我扩展默认使用的实现时,我只能在 Grails 4.0.0.M2上使用它。然后继续从 config (yaml) 中解析 maxFileSize 限制。
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 中有以下内容;
// catch exception when max file size is exceeded
multipartResolver(MyMultipartResolver)
您需要随后检查控制器中的FILE_SIZE_EXCEEDED_ERROR属性并进行相应处理。