Grails3 file upload maxFileSize limit

Naman Jain picture Naman Jain · Apr 24, 2015 · Viewed 10.2k times · Source

I am trying to update the file upload maxFileSize limit in Grails 3 and tried the configuration in src/main/resources/application.properties, application.groovy and application.yml, but it's still throwing the exception

Class
    org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException
Message
    the request was rejected because its size (154738) exceeds the configured maximum (128000)

Any help on where and what to configure in a Grails 3 application?

Answer

lifeisfoo picture lifeisfoo · Nov 4, 2015

Grails 3 and 4

The solution is to set maxFileSize and maxRequestSize limits inside your application.yml file. Be sure to have something like this:

grails:
    controllers:
        upload:
            maxFileSize: 2000000
            maxRequestSize: 2000000

Replace 2000000 with the number of max bytes you want for a file upload and for the entire request. Grails 3 and Grails 4 default is 128000 (~128KB).



FAQ

1. How to convert from MB to bytes?

YOUR_MAX_SIZE_IN_MB * 1024 * 1024 = BYTES

E.g. Convert 5 MB in bytes

5 * 1024 * 1024 = **5242880** bytes

2. Security

As quote from OWASP

Limit the file size to a maximum value in order to prevent denial of service attacks

So these limits exist to prevent DoS attacks and to enforce overall application performance (bigger request size == more memory used from the server).

3. What is the right value for maxFileSize and maxRequestSize?

It depends on the application type, but it's generally a bad idea to keep them too high. If you really need to upload big files, probably you should develop (or use) a dedicated (and separated) service.