?
Solved

upload.getFileSizeMax()>10000

Posted on 2008-10-16
10
Medium Priority
?
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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 28

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 28

Accepted Solution

by:
rrz earned 200 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 28

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Suggested Courses

770 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