javafile-uploadstruts2multiple-file-upload

How to upload multiple files in Struts 2


I have a JavaScript which does multiple uploads of files to my gallery system.

When the button start upload is clicked, it calls multiple threads to my method in an Action class of Struts 2.

In this method, I need to verify if my gallery system has a space to accept a file or not (For this purpose I use a Business Object and then a DAO class).

But my problem is:

When I do that, my action threads execute the validation almost at the same time, but if the thread one didn't finish uploading, then the size of other threads got not correctly.

How to do multiple file uploading in Struts 2?


Solution

  • The Struts 2 framework provides built-in support for processing file uploads that conform to RFC 1867.

    You don't need to use multiple requests/threads to upload multiple files. Each request is processed as a single file uploading which concurrently using a threadpool of the web server. The server is picking a thread per request from any client. So if you uploading multiple files by single file upload you can't control the space required to process all files. Any of the single file uploads might fail due system inability to accept the file and it's not concerned to particular client.

    If you need to upload all multiple files at once then you should use Multiple File Uploading.

    As mentioned in the previous section one technique for uploading multiple files would be to simply have multiple form input elements of type file all with different names. This would require a number of setter methods that was equal to 3 times the number of files being uploaded. Another option is to use Arrays or java.util.Lists.


    You can configure fileUpload interceptor to limit the size of the uploading file

    maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this is not related to the various properties found in struts.properties. Default to approximately 2MB.


    Then you can configure custom messages returned from the interceptor.

    This interceptor will add several field errors, assuming that the action implements ValidationAware. These error messages are based on several i18n values stored in struts-messages.properties, a default i18n file processed for all i18n requests. You can override the text of these messages by providing text for the following keys:

    • struts.messages.error.uploading - a general error that occurs when the file could not be uploaded

    • struts.messages.error.file.too.large - occurs when the uploaded file is too large

    • struts.messages.error.content.type.not.allowed - occurs when the uploaded file does not match the expected content types specified

    • struts.messages.error.file.extension.not.allowed - occurs when the uploaded file does not match the expected file extensions specified


    Multiple files are uploaded by the single request. To control a total size of the request you can use a constant or property struts.multipart.maxSize. For example the constant sets a limit to 1M per request.

    <constant name="struts.multipart.maxSize" value="1000000" />