java - 如何在Struts 2中上传多个文件

标签 java file-upload struts2 multiple-file-upload

我有一个 JavaScript,可以将文件多次上传到我的画廊系统。

当按钮start upload时单击后,它会在 Action 中调用我的方法的多个线程Struts 2 类。

在此方法中,我需要验证我的图库系统是否有空间来接受文件(为此,我使用业务对象,然后使用 DAO 类)。

但我的问题是:

当我这样做时,我的操作线程几乎同时执行验证,但如果线程没有完成上传,那么其他线程的大小就会不正确。

如何在Struts 2中进行多个文件上传?

最佳答案

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

您不需要使用多个请求/线程来上传多个文件。每个请求都作为单个文件上传进行处理,同时使用 Web 服务器的线程池。服务器为来自任何客户端的每个请求选择一个线程。因此,如果您通过单个文件上传来上传多个文件,您将无法控制处理所有文件所需的空间。任何单个文件上传都可能因系统无法接受该文件而失败,并且与特定客户端无关。

如果您需要一次上传所有多个文件,那么您应该使用 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.

<小时/>

您可以配置fileUpload interceptor限制上传文件的大小

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.

<小时/>

然后您可以配置从拦截器返回的自定义消息。

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

<小时/>

单个请求上传多个文件。要控制请求的总大小,您可以使用常量或属性struts.multipart.maxSize。例如,常量将每个请求的限制设置为 1M。

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

关于java - 如何在Struts 2中上传多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44698141/

相关文章:

java - 在 OS X 上 Play Framework 运行时错误

java - maven构建失败但生成了allure报告

java - 如何暂停和播放音乐播放器

java - Android,在tomcat服务器上传一个文件

java - 在保持LogicalTypes的同时将Parquet/Avro GenericRecord写入JSON

gwt - 是否可以通过 GWT RPC Servlet 上传文件?

java - 上传文件和图像

java - 无法在 Struts 2 中更改选择框时重置表单元素的值

struts2 - struts2中struts.xml中动态设置请求参数

java - Struts2 标签 : Fielderrors tag