简易版 文件上传UpdateFile(Struts2版)
2016-05-13 19:34
543 查看
1.搭建环境
eclipase:Luna Release (4.4.0)
struts2 :2.3.28 下载地址:http://download.csdn.net/detail/chuck_kui/9513090
2. 项目结构:
3. 类:
Update.java
Action:TestUpdateAction.java
配置:
struts.xml
upload.jsp
success.jsp
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>效果:
相关文章推荐
- Eclipse窗口总是在最前的解决办法
- JAVA 中消息提示框
- Java设计模式-工厂方法模式和抽象工厂模式
- struts.xml配置详解
- struts.xml配置详解
- 深入理解Java:String
- 利用ajax上传文件到服务器(服务端用的是Struts2)1.
- Java设计模式——装饰者设计模式
- Java知识点思维导视图
- Java 枚举类型
- Java语言的发展简史
- java设计模式—建造者模式
- 在eclipse中,无法识别sun.misc中的类
- Java WebService 简单实例
- java常用设计模式
- 字符串分割--java中String.split()用法
- java 异常 throw 和 throws 的区别
- SpringMVC Controller 介绍
- struts文件上传下载
- 三十三、Java图形化界面设计——布局管理器之null布局(空布局)