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