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);
}
}
}
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);
}
}
}