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

techquesAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rrzConnect With a Mentor Commented:
Sorry I posted information for the wrong method.
You could use  
catch (org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException ex) {
            response.sendRedirect("error.jsp");
}

But if you are trying to limit the size of a file then (in your code at top) instead of
>upload.setSizeMax(10000);
use
upload.setFileSizeMax(10000);
and then use  
catch (org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException ex) {
            response.sendRedirect("error.jsp");
}
0
 
divyeshhdoshiCommented:
will you please produce exception details to resolve the error
0
 
techquesAuthor Commented:
I tried

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

But, SizeLimitExceededException  cannot be resolved in eclipse
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
divyeshhdoshiCommented:
your webserver/website has defined max request size limit.

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

how can the SizeLimitExceededException know when will it throw for more than a defined file size?
0
 
divyeshhdoshiCommented:
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)


0
 
techquesAuthor Commented:
how can I set it

I am using tomcat 6 and servlet.
0
 
rrzCommented:
>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.");
}
0
 
techquesAuthor Commented:
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)

0
 
rrzCommented:
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.
0
All Courses

From novice to tech pro — start learning today.