- 浏览: 108500 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
ldwtill:
overwrite呢?
overload,overwrite和override比较 -
dafeiwang:
...
过滤器和拦截器的区别
实现原理:
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。
具体实现
先要引入必要的jar包:
1、首先,创建文件上传页面index.jsp,内容如下:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- 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>
- <div align="center">
- <!-- enctype="multipart/form-data" 这个属性是必须得有的 -->
- <form action="csdn/upload.action" method="post"
- enctype="multipart/form-data">
- <table border="1px">
- <tr>
- <td>
- 标题:
- </td>
- <td>
- <input type="text" name="title" />
- </td>
- </tr>
- <tr>
- <td>
- 文件:
- </td>
- <td>
- <input type="file" name="pic" />
- </td>
- </tr>
- <tr>
- <td colspan="2" align="center">
- <input type="submit" value="上传" />
- </td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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> <div align="center"> <!-- enctype="multipart/form-data" 这个属性是必须得有的 --> <form action="csdn/upload.action" method="post" enctype="multipart/form-data"> <table border="1px"> <tr> <td> 标题: </td> <td> <input type="text" name="title" /> </td> </tr> <tr> <td> 文件: </td> <td> <input type="file" name="pic" /> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="上传" /> </td> </tr> </table> </form> </div> </body> </html>
备注:
在index.jsp中,先将表单的提交方式设为POST,然后将enctype设为multipart/form-data
2、其次是UserAction.java代码
- package cn.csdn.struts2.action;
- import java.io.File;
- import java.io.IOException;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class UserAction extends ActionSupport{
- /**
- * @author z_xiaofei168
- */
- private static final long serialVersionUID = 1L;
- /** 标题 */
- private String title;
- /** 获取文件 文件名与视图层的名称一致 */
- private File pic;
- /** 获取文件的类型 必须是: 文件名+ContentType */
- private String picContentType;
- /** 获取文件的名称 必须是: 文件名+FileName */
- private String picFileName;
- /** 保存文件路径 /保存文件的文件名称 */
- private String savePath;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public File getPic() {
- return pic;
- }
- public void setPic(File pic) {
- this.pic = pic;
- }
- public String getPicContentType() {
- return picContentType;
- }
- public void setPicContentType(String picContentType) {
- this.picContentType = picContentType;
- }
- public String getPicFileName() {
- return picFileName;
- }
- public void setPicFileName(String picFileName) {
- this.picFileName = picFileName;
- }
- public String getSavePath() {
- /**获取上下的路径*/
- return ServletActionContext.getServletContext().getRealPath(savePath);
- }
- public void setSavePath(String savePath) {
- this.savePath = savePath;
- }
- /** copy读取 */
- public String uploadFile(){
- System.out.println("savePath:"+savePath);
- System.out.println("pic:"+pic);
- System.out.println("picContentType:"+picContentType);
- System.out.println("picFileName:"+picFileName);
- System.out.println("getSavePath():"+getSavePath());
- /**保存的具体路径*/
- String savepath = getSavePath();
- /**根据保存的路径创建file对象*/
- File file = new File(savepath);
- if(!file.exists()){
- /**创建此文件对象路径*/
- file.mkdirs();
- }
- try {
- /**使用的是:org.apache.commons.io.FileUtils FileUtils*/
- FileUtils.copyFile(pic, new File(file,getPicFileName()));
- } catch (IOException e) {
- e.printStackTrace();
- }
- return SUCCESS;
- }
- }
package cn.csdn.struts2.action; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ /** * @author z_xiaofei168 */ private static final long serialVersionUID = 1L; /** 标题 */ private String title; /** 获取文件 文件名与视图层的名称一致 */ private File pic; /** 获取文件的类型 必须是: 文件名+ContentType */ private String picContentType; /** 获取文件的名称 必须是: 文件名+FileName */ private String picFileName; /** 保存文件路径 /保存文件的文件名称 */ private String savePath; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File getPic() { return pic; } public void setPic(File pic) { this.pic = pic; } public String getPicContentType() { return picContentType; } public void setPicContentType(String picContentType) { this.picContentType = picContentType; } public String getPicFileName() { return picFileName; } public void setPicFileName(String picFileName) { this.picFileName = picFileName; } public String getSavePath() { /**获取上下的路径*/ return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } /** copy读取 */ public String uploadFile(){ System.out.println("savePath:"+savePath); System.out.println("pic:"+pic); System.out.println("picContentType:"+picContentType); System.out.println("picFileName:"+picFileName); System.out.println("getSavePath():"+getSavePath()); /**保存的具体路径*/ String savepath = getSavePath(); /**根据保存的路径创建file对象*/ File file = new File(savepath); if(!file.exists()){ /**创建此文件对象路径*/ file.mkdirs(); } try { /**使用的是:org.apache.commons.io.FileUtils FileUtils*/ FileUtils.copyFile(pic, new File(file,getPicFileName())); } catch (IOException e) { e.printStackTrace(); } return SUCCESS; } }
3、配置文件: struts.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
- "http://struts.apache.org/dtds/struts-2.1.7.dtd">
- <struts>
- <package name="csdn" extends="struts-default" namespace="/csdn">
- <global-results>
- <result name="input">/index.jsp</result>
- </global-results>
- <action name="upload" class="cn.csdn.struts2.action.UserAction" method="uploadFile">
- <!-- 文件保存的文件名称 -->
- <param name="savePath">/uploads</param>
- <result name="success">/sc.jsp</result>
- </action>
- </package>
- </struts>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="csdn" extends="struts-default" namespace="/csdn"> <global-results> <result name="input">/index.jsp</result> </global-results> <action name="upload" class="cn.csdn.struts2.action.UserAction" method="uploadFile"> <!-- 文件保存的文件名称 --> <param name="savePath">/uploads</param> <result name="success">/sc.jsp</result> </action> </package> </struts>
4、下面我们就来看看上传成功的页面:
发表评论
-
struts2 工作原理(配合工作原理流程图)
2011-09-12 09:52 1409------------------------------- ... -
Struts通配符
2011-09-09 17:05 972如struts中配有如下几个action,Man ... -
struts2中使用ModelDriven
2011-09-09 16:19 761在Struts 2中,提供了另外一种直接使用领域对象的方式 ... -
OGNL使用小结
2011-09-08 16:24 709最近一直使用struts2,表现层采用JSP,用的st ... -
OGNL 详细资料
2011-05-05 16:46 1034OGNL是XWork引入的一个非常有效的数据处理的工具。我们已 ... -
OGNL学习笔记
2011-05-05 13:00 801Java代码 <%@ page ... -
sruts2.0 web.xml 配置文件
2011-05-04 16:51 1048任何MVC框架都需要与Web ... -
Sturt2-文件上传
2011-05-02 09:55 9081、新建文件夹upload内含upload.jsp,res ... -
Sturt2-文件下载
2011-05-02 09:53 915Java代码 <body> ... -
搭建一个Struts2项目的开发环境,你知道多少呢?
2011-05-02 09:25 821搭建一个Struts2项目的开发环境时,我们一般需要做以下 ... -
仿百度、google分页的实现
2011-05-02 09:23 1092前段时间写了个仿百度、google分页,现 ... -
struts2文件上传的采用的三种方式解析
2011-05-02 09:18 733struts2文件上传的采用的三种方式解析 文件 ... -
基于 Struts 2 拦截器实现细粒度的基于角色的存取控制
2011-05-02 08:55 605本文介绍如何利用 Struts ... -
详解Struts1在web.xml中的基本配置
2011-05-02 08:53 981<servlet> <ser ... -
Struts 工作原理
2011-05-02 08:43 952MVC即Model-View-Controller的 ... -
struts1 struts-config.xml 详细介绍
2011-05-02 08:30 954关于struts1的struts-config.x ... -
struts1 struts2 区别 整理
2011-05-02 08:28 607Action 类:• Struts1要求Act ... -
struts2+fckeditor 注意要点
2011-05-01 18:46 837fckeditor是老外的东西,跟struts2配合时,要多注 ... -
过滤器和拦截器的区别
2011-05-01 18:23 1581Java代码 过滤器和拦截器的区别 ... -
jsp\struts1.2\struts2 中文件上传
2011-05-01 18:21 927刚刚做了三个文件上传的Demo a.在jsp中简单利用Comm ...
相关推荐
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
struts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jarstruts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jar
赠送Maven依赖信息文件:struts2-json-plugin-2.3.24.pom; 包含翻译后的API文档:struts2-json-plugin-2.3.24-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.struts:struts2-json-plugin:2.3.24; ...
struts2-json-plugin,Struts JSON插件
struts2-ssl-plugin-1.2.1.jar
struts2-convention-plugin-2.3.32
struts2-dojo-plugin-2.3.4 jar 日期控件的jar包 需要的自行下载
struts2-convention-plugin-2.3.15.1.jar
struts2-core-2.2.3.1-sources.jar struts2-core-2.2.3.1-sources.jar struts2-core-2.2.3.1-sources.jar
struts2-convention-plugin-2.3.24.1
struts2-dojo-plugin-2.2.1.jar, 直接导入到Struts2项目里即可,注意版本。
-- 为修复struts2 s2-016、s2-017漏洞,重写DefaultActionMapper --> <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class=...
默认的struts2-config-browser-plugin包中的ftl文件include标签路径用的相对路径,会找到包内的include文件,将包内ftl里include的路径改成的/开头的全路径。
struts2-dojo-plugin-2.3.15.3.jar.zip
struts2-jquery-plugin-3.1.0.jar
struts2-core-2.5.18.jar包下载,支持struts2的类库下载
struts2-core-2.5.10.jar ,struts核心包,struts2-core-2.5.10.jar
struts2-jfreechart-plugin-2.1.8.1.jar
struts2-convention-plugin-2.1.6.jar
struts2-spring-plugin-2.3.31.jar