简易版 文件上传UpdateFile(Struts2版)

 1.搭建环境

eclipase:Luna Release (4.4.0)

struts2 :2.3.28  下载地址:http://download.csdn.net/detail/chuck_kui/9513090

2. 项目结构:


3. 类:

Update.java

package com.baidu.update;

import java.io.File;

public class Update {
	private File doc;
	private String  docContentType ;
	private String docFileName;
	private String desc;
	public File getDoc() {
		return doc;
	}
	public void setDoc(File doc) {
		this.doc = doc;
	}
	public String getDocContentType() {
		return docContentType;
	}
	public void setDocContentType(String docContentType) {
		this.docContentType = docContentType;
	}
	public String getDocFileName() {
		return docFileName;
	}
	public void setDocFileName(String docFileName) {
		this.docFileName = docFileName;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	
}

Action: TestUpdateAction.java

package com.baidu.update;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

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

public class TestUpdateAction extends ActionSupport implements ModelDriven<Update>,Preparable {
	
	private static final long serialVersionUID = 1L;
	
	@Override
	public String execute() throws Exception {
		System.out.println(update.getDoc());
		System.out.println(update.getDocContentType());
		System.out.println(update.getDocFileName());
		System.out.println(update.getDesc());
		System.out.println("update: " + update.hashCode());
		System.out.println("栈顶对象: " + ActionContext.getContext().getValueStack().peek().hashCode());
		
		//1. 获得当前web应用所在目录下file文件夹的绝对路径  
		ServletContext servletContext = ServletActionContext.getServletContext(); 
		String dir = servletContext.getRealPath("/files/" +update.getDocFileName());
		
		System.out.println(dir);
		//2.获取输入和输出流
		FileInputStream in = new FileInputStream(update.getDoc());
		FileOutputStream out = new FileOutputStream(dir);
		
		//3.输出文件信息
		byte[] buffe = new byte[1024];
		int len = 0;
		while((len = in.read(buffe)) != -1){
			out.write(buffe, 0, len);
		}
		out.close();
		in.close();
		
		
		return super.execute();
	}

	private Update update;
	@Override
	public Update getModel() {
		update = new Update();
		return update;
	}
	@Override
	public void prepare() throws Exception {
		
	}
	
}

配置:

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>

		<!-- 配置全局的国际化资源文件 -->
	<constant name="struts.custom.i18n.resources" value="nihao"></constant>
	
	<package name="default" namespace="/" extends="struts-default">
	
		<action name="update" class="com.baidu.update.TestUpdateAction">
			<result>/success.jsp</result>
			
			<result name="input">/upload.jsp</result>
		</action>
		
		
	</package>

</struts>
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
   
  <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>
  
</web-app>
页面:JSP

upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<s:form action="update" method="post" enctype="multipart/form-data">
		<s:file name="doc" label="DocFile"></s:file>
		<s:textfield name="desc" label="Desc"></s:textfield>
	
		<s:submit></s:submit>
	</s:form>

</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h4>有为青年</h4>
</body>
</html>
效果:

简易版 文件上传UpdateFile(Struts2版)_第1张图片




你可能感兴趣的:(简易版 文件上传UpdateFile(Struts2版))