struts2多文件上传

package controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport{
	private String username;
	private String upLoadFileFileName[];
	private File upLoadFile[];
	@Override
	public void validate(){
		super.validate();
	}
	public String getUsername(){
		return username;
	}
	public void setUsername(String username){
		System.out.println("setUsername():"+username);
		this.username = username;
	}
	
	
	public String[] getUpLoadFileFileName(){
		return upLoadFileFileName;
	}
	public void setUpLoadFileFileName(String[] upLoadFileFileName){
		this.upLoadFileFileName = upLoadFileFileName;
	}
	public File[] getUpLoadFile(){
		return upLoadFile;
	}
	public void setUpLoadFile(File[] upLoadFile){
		this.upLoadFile = upLoadFile;
	}
	
	@Override
	public String execute() throws Exception{
		System.out.println("username的值是"+username);
		//获得上传后文件要存放的路径
		String targetDirectory=ServletActionContext.getServletContext().getRealPath("/upload");
//		String targetDirectory=ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
		System.out.println(targetDirectory);
		//生成上传的File对象
		System.out.println(upLoadFileFileName);
		for(int i=0;i<upLoadFile.length;i++)
		{
			File target=new File(targetDirectory,new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()).toString()+System.currentTimeMillis()+upLoadFileFileName[i]);
			FileUtils.copyFile(upLoadFile[i],target);
		}
		//复制File对象,从而实现上传文件
		
		return "register";
	}
}

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<package name="struts3.1" extends="struts-default">
		<action name="register" class="controller.Register">
			<result name="register">/showregister.jsp</result>
			<result name="input">/register.jsp</result>
		</action>
	</package>
</struts>

 

<%@ 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>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <s:form action="register" method="post" enctype="multipart/form-data">
    	username:<s:textfield name="username"></s:textfield>
    	<br />
    	<s:file name="upLoadFile"></s:file>
    	<br />
    	<s:file name="upLoadFile"></s:file>
    	<br />
    	<s:file name="upLoadFile"></s:file>
    	<br />
    	<s:submit value="提交"></s:submit>
    </s:form>
  </body>
</html>

 

struts.multipart.maxSize=204800000
struts.multipart.saveDir=/tempUploadFile

 

你可能感兴趣的:(struts2)