struts2 多文件上传

本文详细介绍了多文件上传的处理步骤,包括如何在表单中设置输入字段,使用Action类接收并处理上传的文件,并确保文件正确保存。

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

多文件上传的处理步骤同多文件上传(加入jar文件、写form、写Action)

 

form表单:3个文件的名称要是一样的

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

 

 

Action类:与单文件相比,只是将上传文件和上传文件名变成文件数组,在方法里使用循环进行创建文件

public class Upload {
	
	private File[] image;//此处要与jsp页面上的name的值一样,得到上传的文件

	private String[] imageFileName; //得到文件的名称  命名规则:字段名称+FileName
	
	private String imageContentType;//得到文件的类型   命名规则:字段名称+ContentType

	public String execute() throws Exception {
		
		//得到绝对路径
		String realpath = ServletActionContext.getServletContext().getRealPath("/images"); 
		System.out.println(realpath);
		//上传文件不为空
		if(image != null) {
			//文件夹
			File savedir = new File(realpath);
			//创建文件夹
			if(!savedir.exists()) {
				savedir.mkdirs();
			}
			for(int i = 0 ;i<image.length;i++) {
				File savefile = new File(savedir,imageFileName[i]);
				FileUtils.copyFile(image[i], savefile);
			}
			ActionContext.getContext().put("message", "上传成功");
		}
		return "success";
	}
	
	
	public File[] getImage() {
		return image;
	}

	public void setImage(File[] image) {
		this.image = image;
	}

	public String[] getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String[] imageFileName) {
		this.imageFileName = imageFileName;
	}
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值