I recently developed an online website using tomcat 5.5 as web server, one functionality is that the end user changes their profile and uploads image, when the size of image is 500×600 there have no any problem, when changing the size to 800×800 I ran into java.lang.IllegalStateException: Post too large exception
, I finally determined IllegalStateException: Post too large
is caused by that tomcat has size limitation when uploading attachments and files.
Apache Tomcat by default sets a limit on the maximum size of the accepted post requests, the default configuration of tomcat is set to 2MB, when try to upload file whose size is bigger than 2MB, the error java.lang.IllegalStateException: Post too large exception
occurred.
How to solve IllegalStateException: Post too large?
One approach is to validate size of image on view layer using JavaScript, given an error message if the size of the file is over than 2MB, alternative, we can reconfigure Tomcat accept larger POST requests, either increase the limit or by disable it, open the Tomcat installation dir conf/servler.xml
, find out the following statement then reset maxPostSize value, it limits the maximum post size of its accepted every post requests. setting 0
to disable the size check.
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" maxPostSize="0" />
Then restart tomcat service the error java.lang.IllegalStateException: Post too large
is gone.