You could have a look at using
FileUpload - Home
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(yourMaxMemorySize);
factory.setRepository(yourTempDirectory);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
[b]upload.setSizeMax(yourMaxRequestSize);[/b]
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
However I'm not sure if there is any way for you to check the size before the data has been uploaded to the server, unless you use some sort of client plugin (Flash, Applet, etc.) to check the size first.
// Json