我正在将CommonsMultipartResolver
用于multipart
个文件。我有一个豆子:
@Bean
public CommonsMultipartResolver commonsMultipartResolver() {
final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setMaxUploadSize(maxUploadSize);
return commonsMultipartResolver;
}
然后是一个带方法的控制器:
@RestController
public class Ctrl {
@PostMapping
public ResponseEntity upload(MultipartFile multipartFile) throws MaxUploadSizeExceededException {
...
}
}
异常处理程序:
@RestControllerAdvice(annotations = RestController.class)
public class RestControllerExceptionHandler {
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler({
MaxUploadSizeExceededException.class
})
public ResponseEntity handleBadRequest(final Exception exception) {
...
}
}
我也配置了MultipartFilter
public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer {
@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
insertFilters(servletContext, new MultipartFilter());
}
}
并将Tomcat
配置添加到conf/server.xml
文件maxSwallowSize="-1"
但是当我尝试上传文件>最大尺寸我只是屏幕异常。
HTTP Status 500 - Maximum upload size of 1 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (172065) exceeds the configured maximum (1)
我错过了配置的内容吗?我现在在tomcat级别抛出异常,也许它不可能在Spring控制器建议中捕获它?我发现很少有类似配置的链接,它说它是可能的。