如何在 tomcat servlet 中限制上传文件的大小

how to limit uploaded filesize in tomcat servlet

我需要在 tomcat 上的 servlet 运行 中设置上传文件的最大文件大小。我尝试了在码头上工作的多部分配置,但 tomcat 只是忽略了它。这意味着部署在 tomcat 服务器上导致即使是大文件也可以上传。 我的配置:

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>sk.test.MyServlet</servlet-class>
    <multipart-config>
        <max-file-size>1048576</max-file-size>
        <max-request-size>104857600</max-request-size>
    </multipart-config>
</servlet>

我已经尝试过带注释的配置,但也没有用。

使用:Tomcat 7.0.54,Servlet 3.0

如有任何帮助,我将不胜感激,谢谢

设置最大文件大小的值,在 servlet class 或 web.xml 配置之前使用注释。 请参阅注释中的 maxFileSize 或 xml 配置中的 <max-file-size></max-file-size>

@MultipartConfig(
    location="/tmp", 
    fileSizeThreshold=1024*1024,    // 1 MB
    maxFileSize=1024*1024*5,        // 5 MB 
    maxRequestSize=1024*1024*5*5    // 25 MB
)

<multipart-config>
    <location>/tmp</location>
    <max-file-size>20848820</max-file-size>
    <max-request-size>418018841</max-request-size>
    <file-size-threshold>1048576</file-size-threshold>
</multipart-config>

参考:https://docs.oracle.com/javaee/7/tutorial/servlets011.htm