在Struts 2中上传多个文件 [英] Upload multiple files in Struts 2

查看:98
本文介绍了在Struts 2中上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript,可以将文件上传到我的图库系统.单击开始上传"后,它将在Struts 2的Action类中调用X线程到我的方法.

在这种方法中,我需要验证画廊是否有空间接受文件,为此,我将其传递给业务对象,然后传递给DAO类.但是我的疑问在这里.

当我这样做时,我的X个线程几乎同时执行验证,但是一个线程没有完成上传,因此其他线程获得的大小不是正确的.

有人可以给我一些提示吗?

解决方案

Struts 2框架为处理符合RFC 1867的文件上载提供了内置支持.

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

如果您需要一次上传所有多个文件,则应使用 fileUpload拦截器以限制大小上传文件的

maximumSize(可选)-的最大大小(以字节为单位) 拦截器将允许在操作上设置文件引用.笔记, 这与在中找到的各种属性无关 struts.properties.默认为大约2MB.


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

此拦截器会添加一些字段错误,前提是 操作实现ValidationAware.这些错误消息基于 struts-messages.properties中存储的几个i18n值,默认 已为所有i18n请求处理了i18n文件.您可以覆盖文字 通过为以下键提供文本来显示这些消息:

  • struts.messages.error.uploading-无法上传文件时发生的一般错误

  • struts.messages.error.file.too.large-当上传的文件太大时发生

  • struts.messages.error.content.type.not.allowed-当上传的文件与指定的预期内容类型不匹配时发生

  • struts.messages.error.file.extension.not.allowed-当上传的文件与指定的预期文件扩展名不匹配时发生


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

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


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?

解决方案

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


这篇关于在Struts 2中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆