Announcement Announcement Module
No announcement yet.
In MultipartResolver, different maxUploadSize per user Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • In MultipartResolver, different maxUploadSize per user


    I am building a site that handles file documents that can be uploaded by users.

    The site has upload quota per user, so my Controller must first check for the user (through the session) and then decides the max file size to accept.

    Is that possible using MultipartResolver?
    AFAIK maxUploadSize for MultipartResolver is set in the configuration which is read once during bootstraping. I am not sure if I can change that value per page view basis

    Should I handle the Multipart myself using either FileUpload or COS?

    Thanks in advance

  • #2
    You could extend the CommonsMultipartResolver and override the resolveMultipart() method. Create a local DiskFileUpload instance with the user-specific maxFileSize.

    If you look at the code for that method, a local instance is used if the encoding doesn't match the default encoding. That gives a good example of how to do it.

    The benefit of extending this object would be that you still take advantage of the encoding and file cleanup code.


    • #3
      For me I just don't specify the maxUploadSize of the multipart handler.
      Use your validator to do checking yourself.

      in the validator, do something like:

      if (photoMultipartFile.getSize() > MAX_FILE_SIZE) {
      errors.rejectValue("photo", "error.fileExceedMaxSize", "File size exceed max");
      You have complete control.
      If you want different size for user, you can do this:

      //where userUploadManager.getMaxUploadSize() is how you retrieve the 
      //max upload size, be it from xml, database or whatever.
      int maxSize = userUploadManager.getMaxUploadSize();
      if (photoMultipartFile.getSize() > maxSize) {
      errors.rejectValue("photo", "error.fileExceedMaxSize", "File size exceed max");
      This way you can keep your file format checking, mime type checking any any other check you want, together. Instead of some errors shown as input and others as an error page which is more streamlined in my opinion.

      You don't need to expose the exception handling logic to the Controller, and neither do you need to pass your resolver to your controller directly.

      I personally feel that file exceed size(Unlike DataAccessException and other unforeseen Errors) is a minor input error and does not warrent another jsp page to display it with the possibility of confusing or scaring the user.