我有一个Spring MVC网站,其中包含两个不同的页面,这些页面具有不同的表单来上传不同的文件。其中一个应该有2MB的限制,而另一个应该有50MB的限制。
现在,我在app-config.xml中有这个限制:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes (2097152 B = 2 MB) -->
<property name="maxUploadSize" value="2097152 "/>
</bean>
我可以解决主控制器中的maxUploadSize异常:
@Override
public @ResponseBody
ModelAndView resolveException(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception exception) {
ModelAndView modelview = new ModelAndView();
String errorMessage = "";
if (exception instanceof MaxUploadSizeExceededException) {
errorMessage = String.format("El tamaño del fichero debe ser menor de %s", UnitConverter.convertBytesToStringRepresentation(((MaxUploadSizeExceededException) exception)
.getMaxUploadSize()));
} else {
errorMessage = "Unexpected error: " + exception.getMessage();
}
saveError(arg0, errorMessage);
modelview = new ModelAndView();
modelview.setViewName("redirect:" + getRedirectUrl());
}
return modelview;
}
但显然,这只能控制2MB的限制。我怎么能限制20MB? 我尝试过这个解决方案:https://stackoverflow.com/a/11792952/1863783,它更新了运行时的限制。但是,这会为每个会话和每个控制器更新它。因此,如果一个用户正在上传第一个表单的文件,另一个使用第二个表单上传的文件应该具有第一个表单的限制...
有任何帮助吗?感谢
答案 0 :(得分:3)
这是可能的:覆盖DispatcherServlet
类。一个例子可能会像下面这样说:
public class CustomDispatcherServlet extends DispatcherServlet {
private Map<String, MultipartResolver> multipartResolvers;
@Override
protected void initStrategies(ApplicationContext context) {
super.initStrategies(context);
initMultipartResolvers(context);
}
private void initMultipartResolvers(ApplicationContext context) {
try {
multipartResolvers = new HashMap<>(context.getBeansOfType(MultipartResolver.class));
} catch (NoSuchBeanDefinitionException ex) {
// Default is no multipart resolver.
this.multipartResolvers = Collections.emptyMap();
}
}
@Override
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
if (this.multipartResolvers.isEmpty() && this.multipartResolvers.get(0).isMultipart(request)) {
if (request instanceof MultipartHttpServletRequest) {
logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, "
+ "this typically results from an additional MultipartFilter in web.xml");
} else {
MultipartResolver resolver = getMultipartResolverBasedOnRequest(request);
return resolver.resolveMultipart(request);
}
}
// If not returned before: return original request.
return request;
}
private MultipartResolver getMultipartResolverBasedOnRequest(HttpServletRequest request) {
// your logic to check the request-url and choose a multipart resolver accordingly.
String resolverName = null;
if (request.getRequestURI().contains("SomePath")) {
resolverName = "resolver1";
} else {
resolverName = "resolver2";
}
return multipartResolvers.get(resolverName);
}
}
假设您已在应用程序上下文中配置了多个MultipartResolvers(在示例代码中可能命名为'resolver1'和'resolver2')。
当然,当您在DispatcherServlet
中配置web.xml
时,您使用此类而不是Spring DispatcherServlet
。
另一种方式:也可以使用MultipartFilter。只需覆盖lookupMultipartResolver(HttpServletRequest request)方法并自行查找所需的解析程序即可。但是,有一些ifs和buts。在使用之前查阅文档。
答案 1 :(得分:3)
看起来似乎不是一见钟情的最佳解决方案,但这取决于您的要求。
您可以为所有控制器设置一个具有最大上传大小的全局选项,并使用较低的值覆盖特定控制器。
application.properties文件(Spring Boot)
spring.http.multipart.max-file-size=50MB
使用支票上传控制器
public void uploadFile(@RequestPart("file") MultipartFile file) {
final long limit = 2 * 1024 * 1024; // 2 MB
if (file.getSize() > limit) {
throw new MaxUploadSizeExceededException(limit);
}
StorageService.uploadFile(file);
}
如果文件大于2MB,您将在日志中收到此异常:
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 2097152 bytes exceeded
答案 2 :(得分:0)
这里有一个优雅的解决方案Spring Boot: ClassNotFoundException when configuring maxUploadSize of CommonMultipartResolver
您可以修改应用 application.properties 文件,以根据需要更改最大上传大小
答案 3 :(得分:0)
public class BaseDispatcherServlet extends DispatcherServlet {
@Override
protected void doService(final HttpServletRequest request, final HttpServletResponse response) {
CommonsMultipartResolver multipartResolver = (CommonsMultipartResolver) getMultipartResolver();
if (requestURI.contains("uriWithLargeFileSize")) {
multipartResolver.setMaxUploadSize(100 * 100L);
}
}
}
答案 4 :(得分:0)
作为Commons Fileupload的主要作者和维护者,我将针对您的用例提出以下建议:
Jochen