Struts2 多文件上传

本文介绍如何使用Struts2框架实现多文件上传功能,包括配置最大上传文件大小、定义上传Action处理类及其实现细节、创建上传表单等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        在 struts.xml 中:

			<struts>
				<constant  name="struts.multipart.maxSize=5242880" ></constant>	#控制最大缓存值

				<package name="upload"  namespace="/upload"  extends="struts-default">
					<action name="upload2" class="cn.itcast.action.UploadAction2"  method="execute">
						<result   name="success">/success.jsp</result>
					</action>
				</package>
			</struts>

        在 UploadAction2 中:

			public  class  UploadAction2  extends ActionSupport  implements  Serializable{
				private  File[]  image;		// 对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型
				private  String[]  imageFileName;	// 上传输入域 FileName   即文件名
				private  String[]  imageContentType;	// 文件上传的MIME类型
				封装……

				public  String  execute(){
					try{
						if( image!=null && images.length >0){
							ServletContext   sc = ServletActionContext.getServletContext();
							String  storePath = sc.getRealPath("/files");
							for(int i =0; i<images.length; i++){
								FileUtils.copyFile( image[i],  new File(storePath, imageFileName[i]) );
							}							
						}
						ActionContext.getContext().put("message","上传成功");
						return  SUCCESS;

					} catch(Exception  e){
						return  ERROR;
					}
				}
			}

        在上传页面  upload2.jsp 中

			<form  action="${pageContext.request.contextPath}/upload/upload2"   method="post"  enctype="multipart/form-data">
				文件1:<input  type="file"  name="image"><br/>
				文件2:<input  type="file"  name="image"><br/>
				文件3:<input  type="file"  name="image"><br/>
				<input  type="submit"  value="上传">
			</form>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值