Solved

upload.getFileSizeMax()>10000

Posted on 2008-10-16
10
567 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now