Search code examples
file-uploadconfigurationstruts2

How to fix org.apache.commons.fileupload.FileUploadBase and SizeLimitExceededException in Struts 2


I am trying to upload a file in my application. The file size is of 2055 kb. After uploading the file. It throws an exception is:

04-Feb-2016 15:42:41.141 INFO [http-nio-8084-exec-78] com.opensymphony.xwork2.util.logging.commons.CommonsLogger.info Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
04-Feb-2016 15:42:41.203 WARNING [http-nio-8084-exec-78] com.opensymphony.xwork2.util.logging.commons.CommonsLogger.warn Request exceeded size limit!"
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (2104281) exceeds the configured maximum (2097152)

I am using Struts Framework.


Solution

  • Struts2 allows to configure the limits for uploaded file(s).

    The Struts 2 default.properties file defines several settings that affect the behavior of file uploading. You may find in necessary to change these values. The names and default values are:

    struts.multipart.parser=jakarta
    struts.multipart.saveDir=
    struts.multipart.maxSize=2097152
    

    Note, without 0 at the end.

    You can change this by setting a constant to increase a request limit

    <constant name="struts.multipart.maxSize" value="20971520" />
    

    Please remember that the struts.multipart.maxSize is the size limit of the whole request, which means when you uploading multiple files, the sum of their size must be below the struts.multipart.maxSize!

    There's also a limit on individual file that you can change by overriding the action config

    <interceptor-ref name="fileUpload">
       <param name="maximumSize">20971520</param>
    </interceptor-ref> 
    

    More in formation about File Upload you can find on the docs page.