You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by gus <gu...@spamarrest.com> on 2004/09/28 01:29:14 UTC
Can I detect file size without uploading?
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>
Re: Can I detect file size without uploading?
Posted by struts Dude <sc...@slingshot.co.nz>.
----- Original Message -----
From: "Jason Lea" <ja...@kumachan.net.nz>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Wednesday, September 29, 2004 1:09 PM
Subject: Re: Can I detect file size without uploading?
> struts Dude wrote:
>
>>Cool! I was told 'detect file size without uploading' is impossible.
>>
>>But you said 'might work' above, have you or anyone actually tried this??
>>OK, I am lazy to try this myself, but hey programmers are lazy.
>>
>>
> Well, I'm lazy too :) I just came up with the idea. Whether it works
> is up to the people interested in it to test the idea.
>
>
> But some containers might read the entire request (though I would doubt
> it as that would load it all into memory). I don't need this
> functionality, so I won't be testing it. If you do test it, can you
> post back to the mailing list with your experience so others can see he
> result.
>
No problem, I will post back if I do test it. This may be the only time
I am contributing something useful to this list. ;D
>
> --
> Jason Lea
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can I detect file size without uploading?
Posted by Jason Lea <ja...@kumachan.net.nz>.
struts Dude wrote:
>Cool! I was told 'detect file size without uploading' is impossible.
>
>But you said 'might work' above, have you or anyone actually tried this??
>OK, I am lazy to try this myself, but hey programmers are lazy.
>
>
Well, I'm lazy too :) I just came up with the idea. Whether it works
is up to the people interested in it to test the idea.
From a glance at the servlet spec, this sort of thing might depend on
the implementation. The servlet spec doesn't say the request object
must be completely filled with the entire request when the servlet is
given the object. So I assume it creates a request object, reads the
header and populates it and provides an input stream for the object to
use later. If you access parameters then it has to read in the stream,
and then you must wait for the entire request to be sent. If you just
check the headers then I assume the entire request isn't read and you
can return a response before the entire request is sent by the browser.
But some containers might read the entire request (though I would doubt
it as that would load it all into memory). I don't need this
functionality, so I won't be testing it. If you do test it, can you
post back to the mailing list with your experience so others can see he
result.
--
Jason Lea
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can I detect file size without uploading?
Posted by struts Dude <sc...@slingshot.co.nz>.
----- Original Message -----
From: "Jason Lea" <ja...@kumachan.net.nz>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Wednesday, September 29, 2004 8:57 AM
Subject: Re: Can I detect file size without uploading?
> 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:
Cool! I was told 'detect file size without uploading' is impossible.
But you said 'might work' above, have you or anyone actually tried this??
OK, I am lazy to try this myself, but hey programmers are lazy.
>
> 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: user-unsubscribe@struts.apache.org
>>For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
>>
>
>
> --
> Jason Lea
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Can I detect file size without uploading?
Posted by Jason Lea <ja...@kumachan.net.nz>.
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: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>
>
--
Jason Lea
Re: Can I detect file size without uploading?
Posted by Bj <bj...@free.fr>.
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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org