如何在tomcat servlet中限制上传的文件大小 [英] how to limit uploaded filesize in tomcat servlet

查看:75
本文介绍了如何在tomcat servlet中限制上传的文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

使用:Tomcat 7.0.54,Servlet 3.0

Using: Tomcat 7.0.54, Servlet 3.0

我将不胜感激,谢谢

推荐答案

设置最大文件大小的值,在 servlet 类或 web.xml 配置之前使用注解.参见注释中的 maxFileSize 或 xml 配置中的 .

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
)

<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/教程/servlets011.htm

这篇关于如何在tomcat servlet中限制上传的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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