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?
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 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.
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.
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"
}
}
Try this https://github.com/zyro23/grails-core-668/blob/master/grails-app/conf/application.yml
upload: maxFileSize: 9999999999 maxRequestSize: 9999999999
https://github.com/grails/grails-core/issues/668
grails.controllers.upload.maxFileSize
and/or
grails.controllers.upload.maxRequestSize
config properties/settings
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
来源:https://stackoverflow.com/questions/29845943/grails3-file-upload-maxfilesize-limit