0

I have a JSF application which runs in JBoss 6.1 which uses internal the Tomcat Servlet container.

I've realised the upload with apache commons file upload. I want to prevent too large file uploads and have set the property fileSizeMax to 10MB within the class FileUploadBase. It works, the file upload throws an FileSizeLimitExceededException for all files larger than 10MB. This exception throws within less than a second. But the main problem is, that the whole file will be transferred over the network. I have found this out by checking the network traffic. Afterwards the redirect to the error page is done.

How can I interrupt the file transfer when the max size is exceeded without transferring the whole file? I assume that the file will be transferred in multiple packages because of the web form attribute enctype ="multipart/form-data".

5
  • You mentioned in a comment in the answer that you're using PrimeFaces <p:fileUpload>. You should edit your question to state that you're using <p:fileUpload>, not that you're using Commons FileUpload (using <p:fileUpload> namely already implies for long that you're using Commons FileUpload "under the covers", note that it's thus merely an implementation detail).
    – BalusC
    Jan 25, 2013 at 15:05
  • PrimeFaces is just a detail which is not important beacause I'am using the native upload component from the brwoser without HTML5-JavaScript handling. I'am using a FileUploadFilter in my project which uses the commons file-upload classes to throw an Exception if maximal file size is exceeded.
    – dako-ak
    Jan 25, 2013 at 16:01
  • Oh, then I wonder why you reinvented it instead of using <p:fileUpload mode="simple"> which generates exactly the same HTML and does all the parsing and model-update job fully transparently?
    – BalusC
    Jan 25, 2013 at 16:03
  • I'am using the <p:fileUpload mode="simple"> but I had to get the following Classes into my project because of an UTF-8 bug and to handle exceeding of the max file size. org.primefaces.webapp.filter.FileUploadFilter and org.primefaces.webapp.MultipartRequest. Link to the utf-8 bug: code.google.com/p/primefaces/issues/detail?id=3002
    – dako-ak
    Jan 25, 2013 at 16:31
  • Right, the UTF-8 bug. It's fixed in 3.5 snapshot. See also code.google.com/p/primefaces/issues/detail?id=3002 I've also reported it before stackoverflow.com/questions/11190081/… As to max size, well, see the answer here below.
    – BalusC
    Jan 25, 2013 at 16:35

1 Answer 1

5

You cannot abort a HTTP request halfway. If you did it, you would not be able to return a HTTP response and the client would end up with no form of feedback, expect maybe a browser-specific "Connection reset by peer" error page.

Your best bet is to validate it in JavaScript beforehand. This works by the way only in browsers supporting HTML5 File API. You didn't tell anything about which JSF file upload component you're using, so I have the impression that you just homebrewed one, so I'll give a generic answer which is applicable on the rendered HTML <input type="file"> (note that it works as good on e.g. Tomahawk's <t:inputFileUpload>):

<input type="file" ... onchange="checkFileSize(this)" />

with something like this

function checkFileSize(inputFile) {
    var max = 10 * 1024 * 1024; // 10MB

    if (inputFile.files && inputFile.files[0].size > max) {
        alert("File too large."); // Do your thing to handle the error.
        inputFile.value = null; // Clears the field.
    }
}

In case of older browsers not supporting this, well, you're lost. Your best alternative is Flash or Applet.

1
  • Hi, I'am using PrimeFaces and yes I'am using the simple file upload which means, that the native browser upload is used. I know the Html5-API solutions but for IE this works only in version 10.
    – dako-ak
    Jan 25, 2013 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.