1

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>
Vicky Thakor
  • 3,847
  • 7
  • 42
  • 67

2 Answers2

1

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. i.e. 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.

tom
  • 2,704
  • 16
  • 28
  • I downloaded source file from git https://github.com/manolo/gwtupload and modified the source bit but its giving me strange issue. Check out : http://stackoverflow.com/questions/19064038/the-import-gwtupload-cannot-be-resolved – Vicky Thakor Sep 28 '13 at 07:19
  • Just checked it and I'm glad it's working for you. Please mark the answer as accepted so I can start commenting. Why not just extend the UploadServlet and use your subclass? Perhaps I'm missing something here. – tom Sep 28 '13 at 10:43
  • @tom gwtupload servlet is not thought as a replacement of fileupload library, which actually it uses, but a complement to compute the progress of a upload, able to cancel an ongoing upload, able to send files to GAE etc. It is quite flexible, and as you say in your answer you can override almost behavior extending it to change somethings like dynamically set the max-file size, but notice that the max-size is set fixed because security issues, otherwise anyone could send a malicious request to increase the size of the servlet constrain and ruin your bandwidth or your hosted space quote. – Manolo Carrasco Moñino Oct 09 '13 at 11:02
  • @Manolo true. I'd not recommend the client being able to change it, but it depends on the situation. My max upload is checked at Integer.MAX_VALUE as I have people uploading *huge* files in a trusted environment. It turned out to be fairly simple to write the code that managed progress and had the side benefit of enabling me to audit things better. – tom Oct 09 '13 at 13:46
  • @tom, I'd rather a server check, so depending on an extra parameter in the upload form, use `Uploader.add(new Hidden(...))` method, the server would check which is the max allowed size based in some configuration parameter. It just needs to extend the gwtupload servlet and simply override the `checkRequest(request)` method. I'm a bit paranoiac about security, and follow the principle of DRY though. – Manolo Carrasco Moñino Oct 10 '13 at 07:25
  • My suggestion does pretty much that. You could set an absolute maximum in your configuration file and set an number smaller than that in the a hidden field. That way even if someone does fiddle with the form the servlet is still protected from anything /too/ big. – tom Oct 10 '13 at 10:32
0

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);
}
Sameeh Harfoush
  • 610
  • 1
  • 8
  • 22