`
chenssy
  • 浏览: 61570 次
  • 性别: Icon_minigender_1
  • 来自: 岳阳
社区版块
存档分类
最新评论

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;
	}
}


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



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics