[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1444
  • Last Modified:

jakarta common upload set the file size

Hello, gurus,

I'm using the jakarta common upload files. I want to ask some questions about setting the limition of file size.
The function I'm implementing is : upload a file and save it in server side.
The maxamin file size is : e.g. 2MB=2x1024(KB)=2x1024x1024(Bytes)  // Is this correct ?

The code for limiting file size is following :
<%      
              int maxPostSize = 2x1024x1024;
      DiskFileUpload upload = new DiskFileUpload();
      upload.setSizeThreshold(500x1024);  // so it's 500KB
      upload.setSizeMax(maxPostSize);
                upload.setRepository(new File("/tmp")); // when the file size is lager than 500x1024 bytes, it will be wirtten into
                                                                          //the tmp directory

%>


Question1 : since I just want to write the uploaded file into a specfic directory in the server side, is it necessary that I set upload.setSizeThreshold(500x1024);  in here? Also upload.setRepository(new File("/tmp")); is necessary ? I don't need the file to be written into a temp directory.

Question2: I checked the jakarta API (http://jakarta.apache.org/commons/fileupload/apidocs/), and found that the methods: setSizeThreshold(int sizeThreshold) and setRepositoryPath(java.lang.String repositoryPath) under the class of  DiskFileUpload have been Deprecated.(Use DiskFileItemFactory instead.)

So if I'm right, I should change the above code to
<%
         int maxPostSize = 2x1024x1024;
         DiskFileItemFactory factory = new DiskFileItemFactory();
         factory.setSizeThreshold(500x1024);
        // how to set setSizeMax in here  ?? no method found

%>

Thanks for your help.




0
lilyyan
Asked:
lilyyan
  • 6
  • 5
2 Solutions
 
bloodredsunCommented:
The javadocs do say that this has been depreciated but it does also say "Use ServletFileUpload together with DiskFileItemFactory instead.".

If you use a ServletFileUpload object you can see that it has a setSizeMax() method.
0
 
lilyyanAuthor Commented:
Thanks for your reply.
>>Use ServletFileUpload together with DiskFileItemFactory instead.".

Does that mean it's particuarly used by Servlet? If I use jsp, I need use DiskFileUpload ?

Another question, if the uploaded file will be saved as a file on server side, do I need set something like this:
upload.setSizeThreshold(500x1024);  // memory size
upload.setSizeMax(maxPostSize);  // the max size for requested file
upload.setRepository(new File("/tmp"));




0
 
lilyyanAuthor Commented:
update :

upload.setSizeMax(maxPostSize);  // this is necessary
0
Technology Partners: 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!

 
bloodredsunCommented:
>> If I use jsp, I need use DiskFileUpload ?

You might not know but JSPs are turned into servlets by tomcat so you can happily use ServletFileUpload with no problems. As long is there is a request object then you can use it.
0
 
lilyyanAuthor Commented:
oh, i c

then is it necessary to set the memory size by using upload.setSizeThreshold(500x1024);  and store it into temporary location if the file size larger than 500kb ? my appliaction is to save the file on the server side.

thanks a lot :-)
0
 
bloodredsunCommented:
setSizeThreshold is to do with saving the file to disk temporarily rather than holding it in memory. Yout wouldn't want 10 users each uploading a 50Mb file as it would crach your servers memory. But if you start writing them to disk after 1 Mb/500Kb, your memory would be safe and you'd have no crashes.

It's really just a configuration method to make your webapp more stable.
0
 
lilyyanAuthor Commented:
Hello, thanks for the reply.

I'm confused the process about how a uploaded file is written into a file on server side in the jakarta commom upload package.

Before a file is written into disk, does it need to be stored in the memory first? I don't think so though.
If not, I don't need use setSizeThreshold.

I wish the max. file size is less than 2 MB, so I set upload.setSizeMax(2x1024x1024);

My application is, after uploading a file, the file will be wirtten into another file on server side.
The file need not be hold in the memory.

So I wonder if I need set upload.setSizeThreshold( some bytes). I guess there is a default memory threhold in the jakarta commom upload package. But I didn't find a description under DiskFileUpload class.

Thanks for your help.
0
 
bloodredsunCommented:
>>Before a file is written into disk, does it need to be stored in the memory first?
yes and no. It starts of in memory as the request is parsed, this feature is really a "safty-valve" to prevent your machine from being bogged down. Setting the thresholf allows you to deal with really big files in an efficient manner by writing parts of it to disk, that's all

>>So I wonder if I need set upload.setSizeThreshold( some bytes).
It's optional <shrug>...
0
 
lilyyanAuthor Commented:
If I set setSizeMax(1000) //max. file size 1000bytes

Also, I setSizeThreshold(2) // 2 bytes, and uploading a file which is 500bytes, will there be an exception thrown out?
Since the actual file size is greater than 2 bytes, if there is no exception thrown out, could you explain the reason?

Can I think that setSizeThreshold is not only used for memory safety, but also can be used for improving the program performance , even though my application is to written the file into a disk. So it's better to setSizeThreshold.

Tons of thanks for your help.


0
 
bloodredsunCommented:
>>Also, I setSizeThreshold(2) // 2 bytes, and uploading a file which is 500bytes, will there be an exception thrown out?

No, there shouldn't be an exception. setSizeThreshold() is only the threshold at which files are written to a temporary location so no exceptions will be thrown, only the the file will be written to a temporary location.

>>Can I think that setSizeThreshold is not only used for memory safety, but also can be used for improving the program >>performance , even though my application is to written the file into a disk. So it's better to setSizeThreshold.

yes, it's better to set the threshold as it will improve the performance for both memory safety and program performance..
0
 
lilyyanAuthor Commented:
Thanks for your replies.
0

Featured Post

Industry Leaders: 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!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now