Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

upload.getFileSizeMax()>10000

why this code does not get the file size and throw exception?
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart)
{
DiskFileItemFactory factory = new DiskFileItemFactory();			 
factory.setSizeThreshold(10000);
factory.setRepository(teFile);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(10000);
			
if(upload.getFileSizeMax()>10000)
            {
            	response.sendRedirect("error.jsp");
            }

Open in new window

Avatar of divyeshhdoshi
divyeshhdoshi

will you please produce exception details to resolve the error
Avatar of techques

ASKER

I tried

catch (SizeLimitExceededException ex) {
  /* The size of the HTTP request body exceeds the limit */
}

But, SizeLimitExceededException  cannot be resolved in eclipse
your webserver/website has defined max request size limit.

so you can not upload file more than defined size limit.
if not define upload.setSizeMax(10000);

how can the SizeLimitExceededException know when will it throw for more than a defined file size?
it is in your configuration file of webserver/ website

if it is not defined there then it will take default size according to your webserver/ website.

if you are using asp.net then it is in web.config like maxrequestlength="125478522" (eg no of bytes)


how can I set it

I am using tomcat 6 and servlet.
Avatar of rrz
>why this code does not get the file size and throw exception?  
According to the API the setSizeThreshold method
"Sets the size threshold beyond which files are written directly to disk."
When the threshold is exceeded then the repository is used. From API, the setRepository method  
"Sets the directory used to temporarily store files that are larger than the configured size threshold."

If you want to limit the size of the uploaded file to 10KB, then use something like  
if(request.getContentLength() > 10000){
                out.println("File was too big! Use your back button " + "and a file <" +  10000 + "Bytes.");
}
I added if(request.getContentLength() > 10000)
but it threw
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (611162) exceeds the configured maximum (102400)

ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I find it easier to use
if(request.getContentLength() > 10400){   // added extra to include any parameters that were sent
                                response.sendRedirect("error.jsp");
}
and don't use setFileSizeMax  or  setSizeMax  just leave the defaults.