Struts 2 FileUploadInterceptor fires too late to verify file size and file extension

629 views Asked by At

I can't seem to figure out how to properly use FileUploadInterceptor in Struts2. I have everything wired and it does work. I can specify the file extensions and max file size and it indeed works. The problem is as follows:

  1. I specify the max file size as 100mb
  2. When user uploads 110mb the file I can see that JakartaMultiPartRequest class handles the file upload with the help common file-upload libraries. This happens BEFORE the file upload interceptor.
  3. Once the user waited for minutes and the file upload is done the file upload interceptor fires and tells the user that the file is too big.

There is something wrong with sequence of events. I would like to looks at content-length header and right away tell the user that the file is too big (before uploading). I understand the header is not always there but if it is there I would like to use it.

Other than overwriting JakartaMultiPartRequest class I don't understand how to do this.

Edit: Here is the sequence as I understand it:

  1. User sends multi-part request to server with file upload.
  2. JakartaMultiPartRequest in Struts2 parses the request and uploads it to Struts2 temp folder.
  3. The flow finally reaches the file-upload interceptor where max file size and file extension is checked.

From above it is obvious that the upload happened at step#2. The entire file is now on server and only after that file-upload interceptor detects that it is too big.

0

There are 0 answers