Search code examples
tomcatfile-uploadmultipartlarge-files

How to upload files over 100 MB to Tomcat


I'm facing trouble while uploading large files to the server for my project. I have modified maxPostSize and maxHttpHeaderSize in server.xml as suggested in this article but it's showing java.lang.OutOfMemoryError. What to do?


Solution

  • Have a look at the documentation. In particular, you're interested in the maxFileSize parameter, which you can set either with annotations or xml.

    Directly from the link above,

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

    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>
    

    So for example, you can annotate your servlet like so:

    @MultipartFileConfig( options )
    public class YourServlet extends HttpServlet {
    
        ...
    

    or add the equivalent xml in the config if using xml.