Cancel HttpServletRequest using a Filter

T

Tim

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(ERROR_URI));
return;
}
}

chain.doFilter(request, response);
return;

} catch (Exception ne) {
throw new ServletException(ne);
}
}
}
 
A

Arvind

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(ERROR_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)
 
T

Tim

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top