Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Limit File Size or Request Size

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Limit File Size or Request Size

    Dear all,



    I developed a web application and it is working fine, except for one issue. The application includes uploading files from a JSP to my servlet, and the issue is that i would like to have a limit for the uploaded files on the client side (before actually uploading it).



    I investigated alot and found some ways like changing my JSPs to PHPs, which is not feasable for my application. I would also like to add that using the Flash component ("<object>") for uploading is not feasable also at this time. Using ActiveX does not work also (for some security issues in javascript, it can not access the system information, also ActiveX works only on IE).



    I would like to add that I have my application running on Oracle application Server, JSPs for displaying the forms, MultiPart Java API for getting the form input values and files to my servlet and everything is developed in JAVA.



    I would like some help to solve this problem, either by limiting the file size or the limiting the whole request size sent to the servlet.



    Thanks in advance and I really appreciate your help.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Limit File Size or Request Size

    You can request the file size by creating a File reference, and then calling the length() method.

    File someFile = new File("test.txt");
    System.out.println("The size of test.txt is " + someFile.length());

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Limit File Size or Request Size

    The problem is that i use the <file> tag in HTML to browse for the file. Due to security reasons, the file.value does not return the file path. Therefore, i can not use the above solution.

    Any way, thanks for your help

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Limit File Size or Request Size

    You could have a look at using FileUpload - Home

    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
     
    // Set factory constraints
    factory.setSizeThreshold(yourMaxMemorySize);
    factory.setRepository(yourTempDirectory);
     
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
     
    // Set overall request size constraint
    [b]upload.setSizeMax(yourMaxRequestSize);[/b]
     
    // Parse the request
    List /* FileItem */ items = upload.parseRequest(request);

    However I'm not sure if there is any way for you to check the size before the data has been uploaded to the server, unless you use some sort of client plugin (Flash, Applet, etc.) to check the size first.

    // Json

Similar Threads

  1. Font Size
    By nasi in forum AWT / Java Swing
    Replies: 5
    Last Post: May 13th, 2010, 06:22 AM
  2. [SOLVED] Hashtable and Key Size
    By sapzero in forum Collections and Generics
    Replies: 0
    Last Post: January 28th, 2010, 02:31 PM
  3. [SOLVED] Change the size of an image
    By subhvi in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 23rd, 2009, 11:44 PM
  4. How to set the Listbox size.
    By jacinto in forum AWT / Java Swing
    Replies: 4
    Last Post: June 22nd, 2009, 07:39 AM
  5. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM