在 Spring Boot 中增加 HTTP Post maxPostSize [英] Increase HTTP Post maxPostSize in Spring Boot

查看:77
本文介绍了在 Spring Boot 中增加 HTTP Post maxPostSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的 Spring Boot Web 应用程序,我有一个带有 enctype="multipart/form-data" 表单的 HTML 页面.我收到此错误:

I've got a fairly simple Spring Boot web application, I have a single HTML page with a form with enctype="multipart/form-data". I'm getting this error:

多部分请求包含超出关联连接器上设置的 maxPostSize 限制的参数数据(不包括上传的文件).

The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector.

我使用的是 Spring Boot 的默认嵌入式 tomcat 服务器.显然,默认的 maxPostSize 值为 2 兆字节.有没有办法编辑这个值?通过 application.properties 这样做是最好的,而不必创建自定义 bean 或弄乱 xml 文件.

I'm using Spring Boot's default embedded tomcat server. Apparently the default maxPostSize value is 2 megabytes. Is there any way to edit this value? Doing so via application.properties would be best, rather than having to create customized beans or mess with xml files.

推荐答案

找到了解决方案.将此代码添加到运行 SpringApplication.run 的同一个类中.

Found a solution. Add this code to the same class running SpringApplication.run.

// Set maxPostSize of embedded tomcat server to 10 megabytes (default is 2 MB, not large enough to support file uploads > 1.5 MB)
@Bean
EmbeddedServletContainerCustomizer containerCustomizer() throws Exception {
    return (ConfigurableEmbeddedServletContainer container) -> {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
            tomcat.addConnectorCustomizers(
                (connector) -> {
                    connector.setMaxPostSize(10000000); // 10 MB
                }
            );
        }
    };
}

显然将其添加到您的 application.properties 文件中也会增加 maxPostSize,但我自己没有尝试过所以无法确认.

Apparently adding this to your application.properties file will also increase the maxPostSize, but I haven't tried it myself so I can't confirm.

multipart.maxFileSize=10Mb # Max file size.
multipart.maxRequestSize=10Mb # Max request size.

这篇关于在 Spring Boot 中增加 HTTP Post maxPostSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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