Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java File Upload size Limitation

Posted on 2004-09-22
36
Medium Priority
?
416 Views
Last Modified: 2008-01-09
I am working on a servlet that will do file uploads, and all works fine.  However I want to add a size limitation to it.  I did this, and at first had it upload to the server, then check the size, and if it was too large, had it send back a message to the user saying it was too large.

Then I tried doing a check like this.

if (1048576 < req.getContentLength())
{
  throw custom error message;
}

meaning that if the file was larger then 1mb they would get a custom error.  This works, sort of.  For small files (under a couple mb) it only takes a few seconds, however when I attempted to upload a 1.6gb file (as one of my clients tried to do) the browser sits there and hangs.

Is there a way to force the error to occur and stop parsing the user request?  The first thing I have the service method do is that if statement.  I get the error in my logs instantly, but not to the browser.  Does the client browser get hung up on sending the file, even though I'm not doing anything with it?

I also tried instead of throwing a custom error, I threw an IOException, which didn't work.
I also tried to do a res.sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "File is larger than the set limit");

Which also didn't work.  Is there any way to not have the clients browser choke on this?
0
Comment
Question by:zaq
  • 14
  • 10
  • 9
  • +1
36 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12125951
This works for IE browsers

<html>
<head>
<script language="JavaScript">
function A()
{
var oas = new ActiveXObject("Scripting.FileSystemObject");
var d = document.a.b.value;
var e = oas.getFile(d);
var f = e.size;
alert(f + " bytes");
}
</script>
</head>
<body>
<form name="a">
<input type="file" name="b">
<input type="button" name="c" value="SIZE" onClick="A();">
</form>
</body>
</html>


0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12125980
function a(str)
{
var myFile = new File(str);
     myFile.open("r");
     alert('myFile length: ' + myFile.getLength());

}
0
 

Author Comment

by:zaq
ID: 12126059
I can't use javascript to solve this problem.  Unfortunately the rules of the project do not allow us to do that.

And as for the function a(str), that creates a file, reading in the request content, which is what I'm trying to avoid.  If you read in the request content, when the content is a 1.6gb file, it takes forever.  I can do a req.getContentLength() and instantly know its to big, but I don't know how to stop the client from sending it at that point, if its even possible.
0
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!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12126619
function a(str)
{
var myFile = new File(str);
     myFile.open("r");
     alert('myFile length: ' + myFile.getLength());

}
this is also client side javascript function only, so before uploading only u can get the size of file

Regards
Sudha
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12126641
So you can't use JavaScript. OK. let me check other possibilities



0
 

Author Comment

by:zaq
ID: 12126684
I wanted to test the function you gave me, to see if it would work.  If it did, I could try to convince them to allow javascript for this.  But when I try to use it I get a "File" is undefined.

The activeX version listed before yours works, but ActiveX is definately not going to be allowed.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12126721
If you're handling the upload at the server side, try closing the input stream
0
 

Author Comment

by:zaq
ID: 12126741
well the thing is, I haven't  even opened the input stream.

I do a req.getContentLength(), see its too large, and try to output to the browser, without ever touching the inputstream, but it sits there and hangs while it is aparently recieving/processing the request object, even though I'm not doing anything with it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12126784
OK. Try getting it anyway and closing it
0
 

Author Comment

by:zaq
ID: 12126799
getting it, when the user is trying to upload a 1.6gb file, takes forever.  This time is what I'm trying to avoid.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12126887
>>getting it ... takes forever

What, getting the input stream?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12126944

As mentioned before, it's not possible on all Systems. It is possible on all Windows NT / 2K / XP machines with Internet Explorer. But to use that possibility won't be a good Idea cause it uses Active X and many Apps like Norton think that it's an "evil-script". But there's no other possibility (as far as I know)
Here's a working example:

<html>
<head>
<title>Check Size</title>
<SCRIPT LANGUAGE="VBScript">
<!--
   Function form1_onSubmit ()
          Set WSHShell = CreateObject("WScript.Shell")
          Set fso = CreateObject("Scripting.FileSystemObject")
          Set file = fso.GetFile(document.form1.file.value)
          filename = file.size
          if (file.size > 6291456) then
               MsgBox("The File is too big to upload")
               form1_onSubmit = False
          else
               form1_onSubmit = True
          end if
   End Function
-->
</SCRIPT>


</head>

<body>
<form name="form1" enctype="multipart/form-data" method="post" action="/cgi-bin/upload.cgi">
  <input type="file" name="file">
  <br>
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
0
 

Author Comment

by:zaq
ID: 12126962
Here is my code.

int contentLength = req.getContentLength();
    if (maxSize < contentLength)
    {
      throw new IOException("This file is way too large");
    }

with a 1mb file, it takes about 3 seconds, but with a 1.6gb file, it takes upwards of 10 minutes for the browser to show the error.

if I modify it as follows.

int contentLength = req.getContentLength();
    if (maxSize < contentLength)
    {
      PushbackInputStream input = new PushbackInputStream(
          new BufferedInputStream(req.getInputStream()), 128);
     input.close();
      throw new IOException("This file is way too large");
    }

it still takes the 3 seconds, and 10 minutes respectively.

For both of these, the error log shows the error instantly, but the users browser is hung up for those times.  I don't want the users browser to hang up, even though the server already knows there is an error.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12127112
did u try like this??
int contentLength = req.getContentLength();
    if (maxSize < contentLength)
    {
       response.sendRedirect("YourUploadPage.jsp");
    }
0
 

Author Comment

by:zaq
ID: 12127131
yes.

Ive tried the following inside the IF.

response.sendRedirect()
response.sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE)
throw IOException
return
request = null (didn't figure it would work, but was worth a shot)

All of those had an instant error message in the log files, but the browser was still hung up for the times listed in my previous post.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12127134
If you use the O'Reilly MultipartRequest it should automatically throw an exception if the file > 1Mb
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12127155
That would be in conjunction with this

>>
public MultipartParser(HttpServletRequest req, int maxSize) throws IOException
>>

This is the one you should use
0
 

Author Comment

by:zaq
ID: 12127304
Even the logic out of the multipartParser method causes the same wait/hanging of the users browser.  I will try to incorporate the entire oreilly multipart package tomorrow and use it.  See if that makes things any better or not.  But if useing the logic they use in the parser to send the exception doesn't work in my if statement.  I don't know why theirs would work.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12127341
>>Even the logic out of the multipartParser method causes the same wait/hanging of the users browser

You make it sound as though you've tried this - have you?
0
 

Author Comment

by:zaq
ID: 12127353
yes.  I cut out the logic from their method, and used it as the very first thing in the service method of my file.

caused the same hang up.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12127373
I see, you've got the source?
0
 

Author Comment

by:zaq
ID: 12127389
found it online.  had the jar file of the com.oreilly package, and the java files to go along with it.
0
 
LVL 92

Expert Comment

by:objects
ID: 12128907
I don't believe you can avoid what you are experiencing.
0
 

Author Comment

by:zaq
ID: 12132247
So no matter what I do, unless I use client side activeX objects, there is no way for the client to not experience a delay based on the file size they are trying to upload?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12132267
Have a look to see if there's a way this can be intercepted and disallowed by the web server (if there *is* a layer in front of the servlet engine)
0
 

Author Comment

by:zaq
ID: 12132278
There might be something internal to Tomcat, for a request size limitation, but I haven't looked into tomcat yet to find out.  That I guess will be the next step.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12136244
An applet might be helpfule. But you should sign that

import java.applet.*;
import java.awt.*;
import java.io.*;

public class FileSizeApplet extends Applet {
    public void init() {
        setSize(0,0);
    }
   
   
    public long getFileSize(String str)
    {
        File f=new File(str);
        return f.length();
    }
   
}



<HTML>

<script Language=JavaScript>
            function fileSize()
            {

                  alert(document.FileSize.getFileSize("c:\\a.txt"));
            }
            </script>
<HEAD>
   <TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY >
<APPLET CODE = "FileSizeApplet.class" WIDTH = 10 HEIGHT = 10 NAME = FileSize>
</APPLET>
<form name=filesize1>

<input type=button value='Get Size' onClick="fileSize();">
</form>
</BODY>
</HTML>
0
 

Author Comment

by:zaq
ID: 12136291
applets are yet another thing we aren't allowed to use.

It looks like I'll be able to protect the server from the large files being uploaded, but the client will have to wait to get the error until the upload of the request is complete.  Guess it serves the client right for doing something they know they aren't supposed to.

If anyone else has any ideas I'm all open, but it doesn't seem like there is.
0
 
LVL 92

Expert Comment

by:objects
ID: 12138559
>  there is no way for the client to not experience a delay based on the file size they are trying to upload?

I believe that is correct.
0
 

Author Comment

by:zaq
ID: 12251184
I have to say that the solutions here were good, if not for the limitations of my clients.

I have come to the conclusion that I will do the check in the java, and the client will have to live with the lag time due to uploading a file they should know is too large.

I'm not sure how to go about giving the points for this since I didn't actually get a final workable solution.  Anyone have any idea what I should do about setting this to closed, and giving someone credit for answering?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 800 total points
ID: 12251604
I've no objections to your closing it. Post a zero-pointer to http://www.experts-exchange.com/Community_Support/ and they'll give you your points back
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 12254447
The answer to your question is that there is no way :)
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 1000 total points
ID: 12256107
The answer to your question is either JavaScript(Works for IE only) or Applets. But I do not have any objections on whatever the decision you will take.

Regards
Sudhakar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12258057
8-)
0
 

Author Comment

by:zaq
ID: 12258068
I thought splitting the points between the three of you, since you three tried as much as you could to help would be fair.

Thanks for all the help.  Now I just need to sit down with my clients and tell them that if they want the lag time to go away they must allow javascript.

Thanks again.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12258099
Thank You :-)
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!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

876 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