You could shortcut the process using a filter.

You create a filter that checks the content length of a request, if it is above the threshold intead of passing the request through the normal path, you forward the request onto another action that writes out an error message without consuming the whole request.

Something like this might work:

public class FileUploadSizeCheck implements Filter {
   int maxLength = 1000000;

   public void destroy() {
   }

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException {
if (req.getContentLength() > maxLength) {
request.getRequestDispatcher("/some/new/destination").forward(req, res);
} else {
chain.doFilter(new UploadRequestCounter((HttpServletRequest)req),res);
}
}


   public void init(FilterConfig arg0) throws ServletException {
   }
}



Bj wrote:

Hi,

I have tried several ways to limit upload size but none seems to work.
Each time, the request uploads the whole file before detecting it's too large.
The only way to avoid this may be to configure the web/JSP server himself if it's possible (check but I think tomcat 5 doesn't)
Or to find a browser which implements MAXLENGTH attribut (none ?).
Or to use an applet or an activeX.


Bj

gus a écrit :



I'm using a Struts ActionForm to upload a file using a "multipart/enc"
POST.  The files being uploaded are sometimes rather large and we
wish to restrict them to ... 5 to 10 megs.

Currently we are doing this:
--- inside the effected tile
<html:form method="post" action="/processUploadAction" enctype="multipart/form-data">
<html:file property="attachment" accesskey="f"/>
...
</html:form>



--- inside execute() for Action processUploadAction ... final FormFile attachment = uploadForm.getAttachment(); int attachSize = attachment.getFileSize(); if (attachSize > MAXATTACHSIZE){ actionErrors.add("error.attach.toobig"); return request.findForward("fail"); } ...


Now the issue with this occurs when a user uploads a large file. Let's say they are uploading a 90 megabyte file. This takes 30 minutes. Our session timeout is set to 25 minutes. The user is logged out, we get errors, and the operation fails miserably.

The ideal situation would allow for me to detect the file upload size
before the user uploads, and before they time out.

The following issues prevent me from being able to exhibit the ideal
situation:

1.)  By the time I have reached the action the post has already
taken place.

2.)  Even in the ActionForm.validate method,  I can't see the
size of the file before it has been uploaded.

3.) Using the controller setting, "max-file-size" seems the viable means
to handle this, but unfortunately, this seems to still not restrict the
established rule before the file has been completely uploaded. Additionally,
it is not clear to me as to what the controller does when this max-file-size
threshold is reached -- how do I intelligently produce an error before
my action is touched?



Ideally, what should be happening is we should be able to detect how large the file being uploaded is before the post even occurs. For example, we could look at "request.getContentLength()" and do something smart based on that.


Essentially, it looks as though I may have to create a custom request processor for my purposes because the existing functionality afforded by the Controller is inadequate for our designs. I wish to make sure that this is the case. If this is the case I take it I should start with writing an extended RequestProcessor class that overrides the processPreprocess method and then performs operations on the actual parameters/request object to make sure the content length is short enough.

Any thoughts and help is appreciated.
------------------------------------------------------------------
Gregory Class <http://gregoryclass.com>
Developer
Spam Arrest LLC <http://spamarrest.com>





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]






--
Jason Lea




Reply via email to