- 浏览: 132893 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
-
hongfeng1126:
<form action="/user/log ...
再次发现一个struts2中命名空间有趣的问题 -
accphc:
...
关于客户端和服务器端数据验证的问题 -
fish2007:
知道strtus2 的默认命名空间和 跟命名空间分别在什么地方 ...
再次发现一个struts2中命名空间有趣的问题 -
justry:
高手多多啊。
楼主,写一篇 struts1 的标签 和str ...
再次发现一个struts2中命名空间有趣的问题 -
yojiwei:
我晕,竟然读到两篇相同的文章!!!
Java 泛型的理解与等价实现
struts2没有提供自己的请求解析器,也就是说,struts2不会自己区处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来,但struts2在原有的上传解析器上作了进一步封装,更进一步简化了文件上传
Struts2的struts.properties配置文件中,配置struts2的上传文件解析器
struts.multipart.parser=jakarta (srtuts2默认),也可以设置为常用的cos,pell等
配置上传页面:
配置上传action
package lee;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
...{
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
...{
this.savePath = value;
}
private String getSavePath() throws Exception
...{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) ...{
this.title = title;
}
public void setUpload(File upload) ...{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return (this.title);
}
public File getUpload() ...{
return (this.upload);
}
public String getUploadContentType() ...{
return (this.uploadContentType);
}
public String getUploadFileName() ...{
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
...{
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
...{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}
}
struts配置文件
<?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>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
</package>
</struts>
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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这里web.xml多配置了一个ActionContextCleanUp的配置,这个类是一个filter,他的作用是方便strut2与 sitemesh整合,与文件上传本没有关系,但不加载这个filter,可能在上传出出现莫名的异常,加入后就稳定了,可能是strut2的bug吧
文件上传工程页面:
<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%...@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value=" + title"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
</body>
</html>
运行,选择一个jpg图片,提交,就可以看到上传的页面显示出来了
通常,我们希望限定上传文件的类型,比如说只能上传图片,还需要进一步改造
首先改造Action,增加一个属性和一个方法,属性表示允许上传的文件类型,方法进行验证,是否允许上传
package lee;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
/** *//**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class UploadAction extends ActionSupport
...{
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String allowTypes;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
...{
this.savePath = value;
}
private String getSavePath() throws Exception
...{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) ...{
this.title = title;
}
public void setUpload(File upload) ...{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return (this.title);
}
public File getUpload() ...{
return (this.upload);
}
public String getUploadContentType() ...{
return (this.uploadContentType);
}
public String getUploadFileName() ...{
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
...{
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
//判断是否允许上传
String filterResult=filterType(this.getAllowTypes().split(","));
if(filterResult!=null)...{
ActionContext.getContext().put("typeError", "您要上传的文件类型不正确");
return filterResult;
}
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
...{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}
public String filterType(String[] types)...{
String fileType=this.getUploadContentType();
for(String type:types)...{
if(type.equals(fileType))...{
return null;
}
}
return INPUT;
}
public String getAllowTypes() ...{
return allowTypes;
}
public void setAllowTypes(String allowTypes) ...{
this.allowTypes = allowTypes;
}
}
修改struts.xml定义文件类型及错误返回页面:
<?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>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<param name="allowTypes">image/bmp,image/jpg,image/png,image/gif,image/jpeg</param>
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
修改上传页面:upload.jsp
<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%...@taglib prefix="s" uri="/struts-tags"%>
<%...@ page isELIgnored="false" %>
<%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
${requestScope.typeError}
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="text" name="title" /><br>
<input type="file" name="upload" /><br>
<input value="upload" type="submit" />
</form>
</body>
</html>
文章来源:http://blog.csdn.net/daryl715/archive/2008/02/28/2127229.aspx
Struts2的struts.properties配置文件中,配置struts2的上传文件解析器
struts.multipart.parser=jakarta (srtuts2默认),也可以设置为常用的cos,pell等
配置上传页面:
引用
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>简单的文件上传</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br>
选择文件:<input type="file" name="upload" /><br>
<input value="上传" type="submit" />
</form>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>简单的文件上传</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br>
选择文件:<input type="file" name="upload" /><br>
<input value="上传" type="submit" />
</form>
</body>
</html>
配置上传action
引用
package lee;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
...{
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
...{
this.savePath = value;
}
private String getSavePath() throws Exception
...{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) ...{
this.title = title;
}
public void setUpload(File upload) ...{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return (this.title);
}
public File getUpload() ...{
return (this.upload);
}
public String getUploadContentType() ...{
return (this.uploadContentType);
}
public String getUploadFileName() ...{
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
...{
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
...{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}
}
struts配置文件
引用
<?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>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
</package>
</struts>
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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这里web.xml多配置了一个ActionContextCleanUp的配置,这个类是一个filter,他的作用是方便strut2与 sitemesh整合,与文件上传本没有关系,但不加载这个filter,可能在上传出出现莫名的异常,加入后就稳定了,可能是strut2的bug吧
文件上传工程页面:
引用
<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%...@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value=" + title"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
</body>
</html>
运行,选择一个jpg图片,提交,就可以看到上传的页面显示出来了
通常,我们希望限定上传文件的类型,比如说只能上传图片,还需要进一步改造
首先改造Action,增加一个属性和一个方法,属性表示允许上传的文件类型,方法进行验证,是否允许上传
引用
package lee;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
/** *//**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class UploadAction extends ActionSupport
...{
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String allowTypes;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
...{
this.savePath = value;
}
private String getSavePath() throws Exception
...{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) ...{
this.title = title;
}
public void setUpload(File upload) ...{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return (this.title);
}
public File getUpload() ...{
return (this.upload);
}
public String getUploadContentType() ...{
return (this.uploadContentType);
}
public String getUploadFileName() ...{
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
...{
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
//判断是否允许上传
String filterResult=filterType(this.getAllowTypes().split(","));
if(filterResult!=null)...{
ActionContext.getContext().put("typeError", "您要上传的文件类型不正确");
return filterResult;
}
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
...{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}
public String filterType(String[] types)...{
String fileType=this.getUploadContentType();
for(String type:types)...{
if(type.equals(fileType))...{
return null;
}
}
return INPUT;
}
public String getAllowTypes() ...{
return allowTypes;
}
public void setAllowTypes(String allowTypes) ...{
this.allowTypes = allowTypes;
}
}
修改struts.xml定义文件类型及错误返回页面:
引用
<?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>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<param name="allowTypes">image/bmp,image/jpg,image/png,image/gif,image/jpeg</param>
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
修改上传页面:upload.jsp
引用
<%...@ page language="java" contentType="text/html; charset=GBK"%>
<%...@taglib prefix="s" uri="/struts-tags"%>
<%...@ page isELIgnored="false" %>
<%...@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
${requestScope.typeError}
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="text" name="title" /><br>
<input type="file" name="upload" /><br>
<input value="upload" type="submit" />
</form>
</body>
</html>
文章来源:http://blog.csdn.net/daryl715/archive/2008/02/28/2127229.aspx
发表评论
-
doubleselect:如何数据从数据库里提取
2008-04-16 20:40 4013今天在做PetStore时要用到doubleselect,但数 ... -
Struts 2, 值在验证失败回到原来页面的时候会丢失的解决方案
2008-03-23 17:18 3404比如,当我要添加一个信用卡的时候,我需要信用卡的CardTyp ... -
OGNL表达式语言浅谈
2008-03-21 22:49 3624OGNL(Object Graphic Navigation ... -
如何正确理解拦截器的实现
2008-03-17 18:22 1175拦截器与aop是密切相关的,从某种程度上说,两者可以等同起来. ... -
再次发现一个struts2中命名空间有趣的问题
2008-03-15 13:34 3492今天在处名命空间时,发现一个奇怪的现象,之前我写过关于它的文章 ... -
struts2与servlet API的三种解偶方法
2008-03-13 18:12 2020如果开发过struts1.0的朋友应该知道其与servlet ... -
struts2中一个值得注意的地方
2008-03-10 13:29 1077一个初学者很容易出错的地方是,struts中路径的问题.比如s ... -
Spring 配XML的十二种技巧!
2008-02-28 16:02 936Spring是一个强有力的java ... -
一个不必再使用native2ascii编码的eclipse插件
2008-02-27 19:55 3821和大家分享一个不错的编写properties文件的Eclips ... -
关于客户端和服务器端数据验证的问题
2008-02-24 09:54 1687记得以前学习struts1.3的时候,很不明白,为什么要把验证 ... -
strtus2.0中类型转换与输入校验的流程
2008-02-23 16:29 1017类型转换与输入校验的流程: 1.首先Struts2对客户端伟 ... -
strtus2.0验证框架中的一个非常规问题
2008-02-21 18:51 1434今天在开发一个用户登陆验证时,发现一个非常有意思的问题,我的L ...
相关推荐
阅读《Struts2权威指南》等书籍,访问官方文档(http://struts.apache.org/),以及在博客网站(如iteye.com)上查找相关的学习笔记和实战案例,都能帮助深入理解和掌握Struts2框架。在实际项目中实践,将理论知识...
"struts2权威指南.pdf"很可能是一部全面的Struts2参考书籍,包含框架的配置、拦截器、动作类、结果类型、异常处理等多个方面的内容。"传智播客_spring__黎活明(很经典).pdf"虽然不是直接关于Struts2,但Spring框架与...
"struts2权威指南.pdf"这本书可能是对Struts2框架深入理解的重要资源,其中包括了Struts2的配置、MVC模式、拦截器、结果类型等内容,可以帮助开发者更全面地理解框架的工作原理和解决404错误的方法。"zixue"文件名...
该学习资料包括以下内容: chap01 领略清晰的MVC流程----Struts讲解技术资料.pdf chap02 体验控制反转的理念----Spring介绍技术文档.pdf chap03 感悟Hibernate...尚学堂-马士兵Struts2教学视频之Struts2_学习笔记.pdf
不错的项目源码mysql数据库(教学光盘)(1) struts2权威指南(1) 15天学会jquery(1) struts2+hibernate+spring经典整合实例教程(1) spring框架案例学习文档笔记(1) ext_js(1) struts2,spring
本篇实战笔记主要补充了在JavaEE5中使用JSF的一些关键点,特别是关于流程转向和界面参数传递。 1. **流程转向**: 在JSF中,流程转向通常通过`faces-config.xml`文件中的`<navigation-rule>`元素来定义。例如,当...
- **《OracleDatabase11g性能优化攻略》** 和 **《MySQL5权威指南(第3版)》** 等书籍提供了关于数据库管理和优化的专业知识,对于后端开发者来说非常重要。 - **《MongoDB权威指南》** 介绍了 MongoDB 这一非...