Filter for limiting the file size on upload in Jersey
I have a HTML form that will upload a file (among other form fields) to my server. There, I am using Java and Jersey, and have created a filter which will throw an Exception if the file is to large (>10MB). But I can't seem to get the browser to stop the file upload as soon as I throw the exception. I can see that the reading from the InputStream stops after 10MB at the server side, but the browser will still continue to upload the file until it is finished. Only then will the error be shown.
Is there something with the way HTTP and browsers work that prevents this from working?
The user experience becomes worse if the user have to wait for the while file to be uploaded before getting the error.
This is the Jersey-filter I am using. This seems to be functioning correctly on the server side, so the problem seems to be with the browser.
import java.io.IOException;
import org.apache.commons.fileupload.util.LimitedInputStream;
import org.springframework.stereotype.Component;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
import com.sun.jersey.spi.container.ContainerResponseFilter;
import com.sun.jersey.spi.container.ResourceFilter;
@Component
public class SizeLimitFilter implements ResourceFilter, ContainerRequestFilter {
@Override
public ContainerRequest filter(final ContainerRequest request) {
LimitedInputStream limitedInputStream = new LimitedInputStream(request.getEntityInputStream(), 1024 * 1024 * 10) {
@Override
protected void raiseError(final long pSizeMax, final long pCount) throws IOException {
// Throw error here :)
}
};
request.setEntityInputStream(limitedInputStream);
return request;
}
@Override
public ContainerRequestFilter getRequestFilter() {
return this;
}
@Override
public ContainerResponseFilter getResponseFilter() {
return null;
}
}
(and yes, I know there exists more sexy and modern solutions for uploading files (Javascript functions), but I need to use an approach that works in IE7 :/ )
Edit:
To clarify: everything works, the only problem is that I can't get the browser to show the error before the whole file is uploaded. Hence if I try to upload a 200MB file, the error won't be shown before the whole file is uploaded, even though my code throws an error just after 10MB...
This is because the browser doesn't look for a server response until AFTER it has sent the complete request.
Here are two ways to stop large file uploads before they arrive at your server:
1) Client-side scripting. An applet (Java file upload) or browser plug-in (Flash file upload, ActiveX, or whatever) can check the size of the file and compare it to what you declare to be the limit (you can use the OPTIONS request for this or just include the maximum size allowed in your form as either a hidden input or non-standard attribute on the file input element).
2) Use a 3rd party data service such as Amazon S3 that allows uploads directly from the browser. Set up the form on the page so that it uploads to the data service, and on success invoke some javascript function you write that notifies your server about an available file upload (with the instance details). Your server then sends a request to the data service to find out the size of the file... if it's small enough you retrieve it from the server side. If it's too large you just send a delete request to the data service and then send your response to the browser (your javascript function) that the file was too large and they need to do it again. Your javascript function then affects the display to notify the user.
链接地址: http://www.djcxy.com/p/91552.html上一篇: 使用ExtJS和Jersey上传文件
下一篇: 在Jersey中限制上传文件大小的过滤器