struts2批量上传文件

web.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  
  <display-name>Struts Blank</display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	
	  <welcome-file-list>
	    <welcome-file>index.jsp</welcome-file>
	  </welcome-file-list>
</web-app>

jsp页面代码:

<script type="text/javascript">
		$(function() {
			$("#button").bind("click",function() {
				var html = $("<input type='file' name='file'>");
                var button = $("<input type='button' name='button' value='删除'><br>");
                
                $("#fileDiv").append(html).append(button);
                
                button.click(function()
                {
                    html.remove();
                    button.remove();
                });
			});
		});
	</script>
	
  </head>
  
  <body>
  	<form action="myTestUploadMulti" method="post" enctype="multipart/form-data">
  		username:<input type="text" name="username" /><br/>
  		<div id="fileDiv">file:<input type="file" name="file"/></div><input type="button" value="添加" id="button"><br/>
  		<input type="submit" value="submit">
  	</form>
  	DiagTools.exe<a href="fileDownLoad">下载</a>
  </body>

 struts.xml文件配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="default" namespace="/" extends="struts-default">
		<action name="myTestUploadMulti" class="cn.bdx.action.UploadAction">
			<result name="success">/main/index.jsp</result>
		</action>
		
			</package>
</struts>

 最后就是action类的代码:

package cn.bdx.action;


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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{

	private String username;
	private List<File> file;
	private List<String> fileFileName;
	private List<String> fileContentType;
	
	@Override
	public String execute() throws Exception {
		System.out.println(ServletActionContext.getServletContext());
		String root = ServletActionContext.getServletContext().getRealPath("/upload");
		System.out.println(root);
		
		
		for (int i=0;i<file.size();i++) {
			
			InputStream is = new FileInputStream(file.get(i));
			OutputStream os = new FileOutputStream(new File(root,fileFileName.get(i)));
			
			System.out.println("fileName = " + fileFileName);
			System.out.println("file=" +file.get(i).getName());
			System.out.println("filePath = " + file.get(i).getPath());
			
			byte[] buffer = new byte[1024];
			int length = 0;
			while(-1!=(length = is.read(buffer, 0, buffer.length))) {
				os.write(buffer);
			}
			os.close();
			is.close();
		}
		return SUCCESS;
	}
	

	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}


	public List<String> getFileFileName() {
		return fileFileName;
	}


	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}


	public List<String> getFileContentType() {
		return fileContentType;
	}


	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	
	
	
}

猜你喜欢

转载自forsave.iteye.com/blog/2281272