struts2单文件上传

package controller;

import java.io.File;

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){
		System.out.println("setUpLoadFileFileName()::"+upLoadFileFileName);
		this.upLoadFileFileName = upLoadFileFileName;
	}
	public File getUpLoadFile(){
		return upLoadFile;
	}
	public void setUpLoadFile(File upLoadFile){
		System.out.println("setUpLoadFile()");
		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);
		File target=new File(targetDirectory,upLoadFileFileName);
		//复制File对象,从而实现上传文件
		FileUtils.copyFile(upLoadFile,target);
		
		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>

 

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>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:submit value="提交"></s:submit>
    </s:form>
  </body>
</html>
 

 

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">
  <!-- 配置struts的预处理和执行过滤器 -->
  <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>
 

你可能感兴趣的:(struts2)