If the size of the file being uploaded exceeds the configured maximum, an IllegalStateException
exception will be thrown.
This happens when you attempt to get the Parts of the request by calling the HttpServletRequest.getParts()
or HttpServletRequest.getPart()
. So the easiest way is to just simply put this into a try-catch
block like this:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Parts parts = null
try {
parts = request.getParts();
} catch (IllegalStateException e) {
// File or request is too big!
// Here you can send back an error message to the client,
// I just send back an HTTP 400 (Bad Request) error page.
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// Process parts...
}