如何在Struts 2中上传多个文件 [英] How to upload multiple files in Struts 2

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

问题描述

我有一个JavaScript,可以将文件多次上传到我的图库系统.

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

单击按钮开始上载时,它将在Struts 2的 Action 类中调用我的方法的多个线程.

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

在这种方法中,我需要验证画廊系统是否具有接受文件的空间(为此,我先使用Business Object,然后再使用DAO类).

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).

但是我的问题是:

当我这样做时,我的动作线程几乎同时执行验证,但是如果一个线程没有完成上传,那么其他线程的大小将不正确.

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 the correct.

有人可以帮助我在Struts2中上传多个文件吗?

Can anyone help me to do multiple file uploading in Struts2?

推荐答案

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

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

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

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.

如上一节所述,用于上传多个文件的一种技术将是简单地使类型为file的多个表单输入元素都具有不同的名称.这将需要大量的setter方法,这些方法等于要上传的文件数量的3倍.另一种选择是使用 Arrays java.util.Lists .


您可以配置 fileUpload拦截器以限制大小上传文件的

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

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.

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

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 -无法上传文件时发生的一般错误

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

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

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

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

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 -当上传的文件与指定的预期文件扩展名不匹配时发生

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


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


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天全站免登陆