20

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?

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Naman Jain
  • 231
  • 1
  • 2
  • 8
  • Setting the limits is easy in application.yml though the difficult part is to handle the exception when the max file size is violated, none of the solutions provided seems to work, though here there is a solution https://mkyong.com/spring/spring-mvc-how-to-handle-max-upload-size-exceeded-exception/ – Pablo Pazos Jul 24 '22 at 04:57

6 Answers6

54

Grails 3, Grails 4, Grails 5 and Grails 6

The solution is to set maxFileSize and maxRequestSize limits inside your application.yml file (usually placed inside grails-app/conf/). 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, Grails 4, Grails 5 and Grails 6 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 OWASP says

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.

lifeisfoo
  • 15,478
  • 6
  • 74
  • 115
  • How does this work with grails 4? https://stackoverflow.com/questions/61892143/multipart-file-upload-maxfilesize-exceeded – Lastone May 19 '20 at 13:32
  • the key of the question is how to handle the SizeLimitExceededException exception, since any value that could be set could also be violated – Pablo Pazos Jul 24 '22 at 05:27
  • no @PabloPazos, the OP asked clearly about how to configure the size limit. You are talking about another topic - exception handling. – lifeisfoo Jul 24 '22 at 13:59
  • The exception will still happen after the configuration is done, just upload a bigger file, so that is still part of the problem – Pablo Pazos Jul 24 '22 at 20:11
7

Part of the problem is setting a max file size. The other problem is handling that error gracefully.

Here is my bean definition:

multipartResolver(MyMultipartResolver) {
    maxUploadSize = (5*1024*1024)
}

Here is my implementation:

public class MyMultipartResolver extends CommonsMultipartResolver {

    static final String FILE_SIZE_EXCEEDED_ERROR = "fileSizeExceeded";

    public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) {
        try {
            return super.resolveMultipart(request);
        } catch (MaxUploadSizeExceededException e) {
            request.setAttribute(FILE_SIZE_EXCEEDED_ERROR, true);
            return new DefaultMultipartHttpServletRequest(request, new LinkedMultiValueMap<String, MultipartFile>(), new LinkedHashMap<String, String[]>(), new LinkedHashMap<String, String>());
        }
    }
}

The idea being to check for the request attribute in your controller or a filter to determine if the file was too large.

James Kleeh
  • 12,094
  • 5
  • 34
  • 61
4

I also had no luck trying to set new maximum values in application.properties or application.yml.

What does work though is using a bean definition in conf/spring/resources.groovy:

import javax.servlet.MultipartConfigElement

// Place your Spring DSL code here
beans = {
   multipartResolver(org.springframework.web.multipart.commons.CommonsMultipartResolver){
        maxInMemorySize=1000000
        maxUploadSize=100000000
        //uploadTempDir="/tmp"
    }
}
Michael B
  • 331
  • 2
  • 6
  • 1
    I tried this which needed commons file-upload library on classpath 'commons-fileupload:commons-fileupload:1.2.1'. It still fails with same exception. Is there any additional settings? – Naman Jain Apr 27 '15 at 09:06
  • I didn't need additional settings, and I also don't need a dependency on commons-fileupload. Something else must be going on in your application. – Michael B Apr 29 '15 at 19:52
  • 1
    I added this: `compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.3'` BTW, only this seemed to work. It ignored the other settings (Grails 3.2.11). – Nathan Dunn Aug 26 '17 at 19:59
  • I used this answer in combination with dependencies comment. Solved the error but now the file in the request is null. – IgniteCoders Apr 11 '18 at 14:36
  • I also get a null file in the request on Grails 3.3.10 – Pablo Pazos Jun 27 '20 at 00:39
3

https://github.com/grails/grails-core/issues/668

grails.controllers.upload.maxFileSize

and/or

grails.controllers.upload.maxRequestSize 

config properties/settings

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Naman Jain
  • 231
  • 1
  • 2
  • 8
3

Try this https://github.com/zyro23/grails-core-668/blob/master/grails-app/conf/application.yml

upload: maxFileSize: 9999999999 maxRequestSize: 9999999999

Ashutosh
  • 39
  • 2
0

the following worked for me(grails 3.3.9):
in conf/spring/resources.groovy

import org.springframework.web.multipart.commons.CommonsMultipartResolver

// Place your Spring DSL code here
beans = {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver()
        multipartResolver.setMaxUploadSize(2000000)
}

in MyFile.groovy domain

package attainrvtwo

class MyFile {

    byte[] myFile

    static constraints = {
        myFile(maxSize: 2000000)
    }
}

at the end of conf/application.yml

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