Upload multiple files in Struts 2 [closed]

匆匆过客 提交于 2020-01-23 17:32:07

问题


I have a javascript which does mult upload of files to my gallery system. When the "start upload" is clicked, it calls X threads to my method in an Action class of Struts 2.

In this method, I need verify if my gallery has space to accept the file or not, for this I pass to a Business Object and then to a DAO class. But my doubt is here....

When I do that, my X threads execute the validation almost at the same time, but the thread one didn't finalize the upload, then the size the others threads got aren't the correct one.

Can anyone give me some tips?


回答1:


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" />



来源:https://stackoverflow.com/questions/44698141/upload-multiple-files-in-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!