根据控制器更改文件大小限制(maxUploadSize) [英] Changing file size limit (maxUploadSize) depending on the controller

查看:2824
本文介绍了根据控制器更改文件大小限制(maxUploadSize)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个不同页面的Spring MVC网站,这些页面具有不同的表单来上传不同的文件。其中一个应该有2MB的限制,而另一个应该有50MB的限制。

I have a Spring MVC web with two different pages that have different forms to upload different files. One of them should have a limitation of 2MB, while the other should have a 50MB limitation.

现在,我的app-config.xml中有这个限制:

Right now, I have this limitation in my 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异常:

And I could resolve the maxUploadSize exception in my main controller like that:

@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;
}

但这显然只能控制2​​MB的限制。我怎么能限制20MB?
我尝试过这个解决方案: https://stackoverflow.com/a/11792952/1863783 ,更新运行时的限制。但是,这会为每个会话和每个控制器更新它。因此,如果一个用户正在上传第一个表单的文件,另一个使用第二个表单上传的文件应该具有第一个表单的限制...

But this, obviously, only controlls the 2MB limit. How could I make the limitation for the 20MB one? I've tried this solution: https://stackoverflow.com/a/11792952/1863783, which updates the limitation in runtime. However, this updates it for every session and every controller. So, if one user is uploading a file for the first form, another using uploading in the second form should have the limitation of the first one...

任何帮助?谢谢

推荐答案

看起来这不是一见钟情的最佳解决方案,但这取决于您的要求。

It may seem like not the best solution at first sight, but it depends on your requirements.

您可以为所有控制器设置一个具有最大上传大小的全局选项,并使用较低值覆盖特定控制器。

You can set a global option with maximum upload size for all controllers and override it with lower value for specific controllers.

应用程序。属性文件(Spring Boot)

application.properties file (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,您将在日志中收到此异常:

In case of a file bigger than 2MB you'll get this exception in log:

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 2097152 bytes exceeded

这篇关于根据控制器更改文件大小限制(maxUploadSize)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆