how to limit uploaded filesize in tomcat servlet

爱⌒轻易说出口 提交于 2020-04-10 04:16:42

问题


I need to set max filesize for uploaded files in my servlet running on tomcat. I tried multipart config which worked on jetty, but tomcat just ignored it. It means deployment on tomcat server caused even big files can be uploaded. My configuration:

<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>

I already tried annotated configuration, which didnt work too.

Using: Tomcat 7.0.54, Servlet 3.0

I will appreciate any help, thanks


回答1:


Set value of max file size, use annotation before servlet class or web.xml config. See maxFileSize in annotation or <max-file-size></max-file-size> in xml config.

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

or

<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>

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



来源:https://stackoverflow.com/questions/28564683/how-to-limit-uploaded-filesize-in-tomcat-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!