Struts 2读书笔记-----使用Struts 2控制多文件上传

          在前面我已经介绍过了利用Struts 2控制单文件上传:Struts 2读书笔记-----使用Struts 2控制文件上传 .其实对于多文件上传也差不多。加入我们要同时控制三个文件进行上传。那么页面得有三个文件上传域。在这里主要介绍采用数字和list来控制文件上传。

 

         一、利用数组

          利用数组来封装3个文件域。为了让数组一次性封装三个文件,我们需要将三个文件域的name属性设置为一样的。如下:

    <form action="upload.action" method="POST" enctype="multipart/form-data">
    	文件标题:<input type="text" name="title"/><Br />
    	第一个文件:<input type="file" name="upload"/><br/>
    	第二个文件:<input type="file" name="upload"/><br/>
    	第三个文件:<input type="file" name="upload"/><br/>
    	<input type="submit" value="上传"/>
    </form>


        在处理上传的过程中,我们需要3个数组来分别封装文件名、文件类型、文件内容

     

public class UploadToArrayAction extends ActionSupport {
	private String title;                          // 文件标题
	private File[] upload;                         // 使用file数组封装多个文件域对应的文件内容
	private String[] uploadContentType;             // 使用字符串数组封装多个文件域对应的文件类型
	private String[] uploadFileName;               // 使用字符串数组封装对个文件域对应的文件名

	private String savePath;                       // 动态设置文件属性

	public String getTitle() {
		return title;                        
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {   
		this.savePath = savePath;
	}

	public String execute() throws Exception{
		//取得需要上传的文件数组
		File[] files = getUpload();
		//遍历每个需要上传的文件
		for (int i = 0; i < files.length; i++) {
			//以服务器的文件保存地址和源文件名建立上传文件输出流
			String root = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
			FileOutputStream fos = new FileOutputStream(root+"\\"+getUploadFileName()[i]);
			//以每个需要上传的文件建立输入流
			FileInputStream fis = new FileInputStream(files[i]);
			//将每个需要上传的文件写入
			byte[] buffer = new byte[1024];
			int length = 0;
			while ((length=fis.read(buffer))>0) {
				fos.write(buffer,0,length);
			}
			fos.close();
		}
		return SUCCESS;
	}
}

   

          struts.xml配置文件的配置

<package name="mystruts" extends="struts-default">
		<action name="upload" class="com.app.action.UploadToArrayAction">
			<!-- 动态设置Action的savePath属性 -->
			<param name="savePath">/upload</param>
			<!-- 配置文件上传的拦截器 -->                       
			<interceptor-ref name="fileUpload">
				<!-- 配置允许上传的文件类型 -->
				<param name="allowedTypes">image/png,image/gif,image/jpeg</param>
				<param name="maximumSize">20480000</param>
			</interceptor-ref>
			
			<!-- 配置系统默认的拦截器 -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<result name="success">/success.jsp</result>
			<result name="input">/upload.jsp</result>
		</action>
	</package>

          配置这个Action与配置单文件上传的Action其实是没有什么其他的区别的。

         

          二、利用List

           其实利用List和利用数组时没有什么区别的。只是将Array数组改为了泛型了list而已了。

         

public class UploadToListAction extends ActionSupport {
	private String title; // 文件标题
	private List<File> upload; // 使用file数组封装多个文件域对应的文件内容
	private List<String> uploadContentType; // 使用字符串数组封装多个文件域对应的文件类型
	private List<String> uploadFileName; // 使用字符串数组封装对个文件域对应的文件名

	private String savePath; // 动态设置文件属性

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String execute() throws Exception {
		// 取得需要上传的文件数组
		List<File> files = getUpload();
		// 遍历每个需要上传的文件
		for (int i = 0; i < files.size(); i++) {
			String root = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
			FileOutputStream fos = new FileOutputStream(root+"\\"+getUploadFileName().get(i));
			FileInputStream fis = new FileInputStream(files.get(i));
			byte[] buffer = new byte[1024];
			int length = 0;
			while((length=fis.read())>0){
				fos.write(buffer,0,length);
			}
			fos.close();
		}
		return SUCCESS;
	}
}


          其他的配置和数组的差不多,就不写了................



 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大明哥_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值