Thanks for your reply Arvind.
I do know that. And Struts (which I am using as a framework) can also
configure a maximum upload size.
But I need to determine the maximum size dynamically at runtime.
For example, if the user has logged in and is a registered user, their
upload limit will be 20 MB, otherwise it will be 4 MB. So a static
configuration setting just won't do.
Any other thoughts?
(Arvind) wrote in message news:< om>...
> (Tim) wrote in message news:<. com>...
> > I'm trying to stop a file upload from a form post if the file size is
> > too large, but I need to do it as early as possible in the upload
> > process.
> >
> > When exactly does the Filter kick in? Does the file upload have to
> > complete before the filter?
> >
> > Here's how things are laid out:
> > 1. JSP form posts multipart request to server
> > 2. Filter kicks in and looks at the content-length header
> > 3. If content-length exceeds limit, redirect to a URL
> >
> > See my code snippet below:
> >
> > public void doFilter(ServletRequest req,ServletResponse
> > res,FilterChain chain) throws IOException,ServletException {
> > HttpServletRequest request = (HttpServletRequest) req;
> > HttpServletResponse response = (HttpServletResponse) res;
> >
> > int contentLength = request.getContentLength();
> > String contentType = request.getContentType();
> >
> > try {
> > // if this is a multipart (file upload) request
> > if (contentLength > 0 && contentType != null
> > && contentType.startsWith("multipart/form-data")) {
> > // compare content-length to max file size
> > if (!acceptFile(request, contentLength)){
> > response.sendRedirect(response.encodeRedirectURL(E RROR_URI));
> > return;
> > }
> > }
> >
> > chain.doFilter(request, response);
> > return;
> >
> > } catch (Exception ne) {
> throw new ServletException(ne);
> > }
> > }
> > }
>
> If you did not know, HttpServers/WebServer typically support the
> ability to configure the size of upload permissible. (e.g. IBM Http
> Server)