Solved

upload.getFileSizeMax()>10000

Posted on 2008-10-16
10
587 Views
Last Modified: 2013-11-24
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

0
Comment
Question by:techques
  • 4
  • 3
  • 3
10 Comments
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22737903
will you please produce exception details to resolve the error
0
 

Author Comment

by:techques
ID: 22737928
I tried

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

But, SizeLimitExceededException  cannot be resolved in eclipse
0
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22737956
your webserver/website has defined max request size limit.

so you can not upload file more than defined size limit.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:techques
ID: 22738079
if not define upload.setSizeMax(10000);

how can the SizeLimitExceededException know when will it throw for more than a defined file size?
0
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22738313
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
 

Author Comment

by:techques
ID: 22738357
how can I set it

I am using tomcat 6 and servlet.
0
 
LVL 27

Expert Comment

by:rrz
ID: 22741658
>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
 

Author Comment

by:techques
ID: 22754962
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
 
LVL 27

Accepted Solution

by:
rrz earned 50 total points
ID: 22755120
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
 
LVL 27

Expert Comment

by:rrz
ID: 22755144
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 135
for loop with Set 4 50
java continue statement 10 139
iterator/ListIterator approach 17 27
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question