码迷,mamicode.com
首页 > Web开发 > 详细

Struts2中实现多文件上传功能

时间:2014-10-31 12:01:11      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:struts2   多文件上传   


上篇理了一下Struut 2实现文件上传功能的思路,下面接着来看一下多文件上传是如何实现的。


1、 多文件上传也是在单文件上传的基础上,稍微修改一下数据类型和实现方法,即可完成,思路都是大致相同的。

2、 仍然是使用上一篇的项目结构和代码,需要修改的只是UploadAction.java、upload.jsp这两个文件。

3、 修改后的UploadAction.java、upload.jsp这两个文件如下: 


UploadAction.java

package com.upload.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{

	private List<File> upload;              //上传文件
	private List<String> uploadFileName;    //上传的文件名
	
	public List<File> getUpload() {
		return upload;
	}

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

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

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

	public String upload() throws Exception{
		
		//如果选择了上传功能,则执行上传操作;否则,不作任何操作
		if(getUpload() != null){	
			
			//遍历,对每个文件进行读/写操作
			for(int i = 0; i < upload.size(); i++){
				//根据上传的文件得到输入流
				InputStream is = new FileInputStream(getUpload().get(i));
				//指定输出流地址,此处是输出到服务器项目的根目录下的images/userPhoto下
				OutputStream os = new FileOutputStream(getWebRootPath() + "images\\userPhoto\\" + getUploadFileName().get(i));
				
				byte buffer[] = new byte[1024];
				int count = 0;
				//把文件写到指定位置的文件中
				while((count = is.read(buffer)) > 0){
					os.write(buffer, 0, count);
				}
				//关闭输出流对象
				os.close();
				//关闭输入流对象
				is.close();
				//返回
			}
			return SUCCESS;
		}
		else {
			return ERROR;
		}
	}
		
	/**
	 * 获得web项目根目录
	 */
	public String getWebRootPath() throws Exception {
		ActionContext actionContext = ActionContext.getContext();
		ServletContext servletContext = (ServletContext)actionContext.get(ServletActionContext.SERVLET_CONTEXT);
		String rootPath = servletContext.getRealPath("/");
		return rootPath;
	}
}

upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <title>文件上传</title>
  </head>
  
  <body>
      <s:form action="upload.action" method="post" enctype="multipart/form-data">
        <div> 
          <label for="photoPath1">上传文件一:</label>
          <input type="text" id="photoPath1" disabled="disabled"/>
          <input type="button" onclick="myfile1.click();" value="选择文件" id="select1"/><br />
          <input type="file" id="myfile1" name="upload"  onchange="photoPath1.value=this.value" style="display:none" />
        </div>
        <div> 
          <label for="photoPath2">上传文件二:</label>
          <input type="text" id="photoPath2" disabled="disabled"/>
          <input type="button" onclick="myfile2.click();" value="选择文件" id="select2"/><br />
          <input type="file" id="myfile2" name="upload"  onchange="photoPath2.value=this.value" style="display:none" />
        </div> 
        <div> 
          <label for="photoPath3">上传文件三:</label>
          <input type="text" id="photoPath3" disabled="disabled"/>
          <input type="button" onclick="myfile3.click();" value="选择文件" id="select"/><br />
          <input type="file" id="myfile3" name="upload"  onchange="photoPath3.value=this.value" style="display:none" />
        </div>    
          <input type="submit" value="上传" />
      </s:form>
  </body>
</html>


修改代码后,通过浏览器访问该项目的upload.jsp即可看到看到上传页面。


Struts2中实现多文件上传功能

标签:struts2   多文件上传   

原文地址:http://blog.csdn.net/wangcunhuazi/article/details/40650895

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!