简体   繁体   中英

gwtupload: Set dynamic file upload size

I'm working around file upload in GWT. My test application working fine. I want to set different file upload limit for different upload widget created by gwtupload . ( Project Home )

SingleUploader sUploader = new SingleUploader();

I set upload limit in web.xml using below code. Is there any option to set different file size at run time?

<context-param>
    <!-- max size of the upload request -->
    <param-name>maxSize</param-name>
    <param-value>3145728</param-value>
  </context-param>

In the gwtupload UploadServlet.java class there's this method you can override which gets called before the request is passed on to org.apache.commons.fileupload.servlet.ServletFileUpload:

public void checkRequest(HttpServletRequest request) {
    logger.debug("UPLOAD-SERVLET (" + request.getSession().getId() + ") procesing a request with size: " + getContentLength(request) + " bytes.");
    if (getContentLength(request) > maxSize) {
      throw new UploadSizeLimitException(maxSize, getContentLength(request));
    }
}

If you need to modify the max allowed size client side via SingleUploader you could do this by passing the maxsize in a hidden form field and checking it in an overridden checkRequest method. The size configured in the web.xml would then be checked a second time but the apache library and be the absolute max size. ie you could restrict to a number /lower/ than that but not higher.

I eventually gave up on gwtupload because of things like this and rolled my own, it was fairly easy using the apache fileupload library and much more flexible.

just override the below. it worked for me, maxSize is a protected variable in superclass

@Override
public void init() throws ServletException {
    maxSize = 157286400;
    super.init();
}

@Override
public void checkRequest(HttpServletRequest request) {
    maxSize = 157286400;
    super.checkRequest(request);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM