How to stop a user to upload big size files?
February 9th, 2011
589 views
Leave a comment
Go to comments
If you are planning to validate a file over its size at client side only using some java script then SORRY.

Listen
Listen
Listen
You need not to be disappointed. I have many solutions
- ActiveX control
Write the following code in script tag in your HTML.function getSize() { var myFSO = new ActiveXObject("Scripting.FileSystemObject"); var filepath = document.upload.file.value; var thefile = myFSO.getFile(filepath); var size = thefile.size; alert(size + " bytes"); }Please note thisUse of ActiveX control is always avoided due to security reasons. - Java Applet – I had written a Java applet. But i’ll prefer to write a separate code for the same.
- SWF – I haven’t tried it before. But it is a very good option.
- .htaccess
Write following line in your .htaccess file.LimitRequestBody 2097152
Apache error log will generate this entry when you exceed this limit on a form post or get request:
Requested content-length of 4000107 is larger than the configured limit of 2097152
And it will also display this message back in the web browser:
<h1>Request Entity Too Large</h1>
By the way, the error number returned is 413. So, you could use a directive in your .htaccess file.
Redirect 413 413.html
- Best way:
- Create a file progress bar who monitors how much part of a file has been uploaded.
- Once it crosses maximum specified size limit;
- Leave writing
- Prompt the client.
- commons-fileupload-1.2.1
- commons-io-1.4
Let client upload the file. Don’t write it at server end immediately. Instead,
Now see how to implement this logic in Java
1
Download following jars
You will get them easily on apache sites.
2
Add following code in your java class say servlet.
import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.*; import org.apache.commons.io.*;
3
Following line will help you to identify whether client is uploaded a file or not.
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
4
If yes then first read all the values through some iterator. And check whether there is any non form field (means whether the current item is a file)
Object img = itr.next();
FileItem item = (FileItem) img;
if (!item.isFormField()){
:
}
5
Now its up to you whether you write a file on your server immediately. Or Byte by Byte. I’ll suggest for second option.
byte[] boundary = "article-stack.net".getBytes();
//byte[] boundary = new byte[1000000]; //geting hanged by defining fixed no of bytes
try{
MultipartStream ms = new MultipartStream(item.getInputStream(),boundary,1000);
FileOutputStream fileOut = new FileOutputStream(savedFile);
ms.readBodyData(fileOut);
fileOut.flush();
fileOut.close();
}catch(Exception exp)
{
savedFile.delete();
exp.printStackTrace();
}
6
Its not done boss. You have to write a progress listener.
ProgressListener progressListener = new ProgressListener(){
private long megaBytes = -1;
public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 1000000;
if (megaBytes == mBytes) {
return;
}
megaBytes = mBytes;
System.out.println("We are currently reading item " + pItems);
if (pContentLength == -1) {
System.out.println("So far, " + pBytesRead + " bytes have been read.");
} else {
System.out.println("So far, " + pBytesRead + " of " + pContentLength
+ " bytes have been read.");
}
}
};
Modify the above progress listener as per your need to limit the file size.
Happy……..

NOT WHOLE, only up to the threshold value.
If you are creating a progress listener which is watching the file how much it is uploaded then you can throw an exception once it crosses threshold size.
Otherwise you would have to use some client side application like SWF,Applet. I already had mentioned it in article.
the key is how to “STOP” uploading. your Java solution responses to the size limit after uploading the whole content of the large files.
Good post.