struts2 基础3 文件上传、拦截器
文件上传:
1、将头设置为enctype=”multipart/form-data”
<form action="${pageContext.request.contextPath }/upload/upload1" method="post" enctype="multipart/form-data">
文件<input type="file" name="image"><br>
<input type="submit" value="上传">
</form>
2、写接收处理的方法,有两种,一种是自己实现IO流,一种是使用FileUtils
package cn.gs.ly; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport {
private File image; //struts2封装好的 上传的文件
private String imageFileName; //上传输入域的文件名
private String imageContentType; //上传文件的MIME类型 //setter/getter方法
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
} public String execute1(){
System.out.println("文件类型:"+imageContentType);
//文件存放的真实路径
String path = ServletActionContext.getServletContext().getRealPath("files");
System.out.println("文件存放路径:"+path);
File f = new File(path);
if(!f.exists()){
f.mkdirs(); //创建一个路径
} try {
// //自己实现IO流 。构建输入输出流
// InputStream in = new FileInputStream(image);
// OutputStream out = new FileOutputStream(f+"\\"+imageFileName);
// byte b[] = new byte[1024];
// int len=-1;
// while((len=in.read(b))!=-1){
// out.write(b, 0, len);
// }
// out.close();
// in.close(); //使用FileUtils
FileUtils.copyFile(image, new File(path,imageFileName)); ActionContext.getContext().put("message", "文件上传成功");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
} }
}
多文件上传:
package cn.gs.ly; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UploadAction2 extends ActionSupport {
private File[] image; //struts2封装好的 上传的多个文件数组
private String[] imageFileName; //上传输入域的文件名
private String[] imageContentType; //上传文件的MIME类型 //setter/getter方法
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
} public String execute2(){
try {
if(image!=null&&image.length>){ //文件存放的真实路径
String path = ServletActionContext.getServletContext().getRealPath("files");
System.out.println("文件存放路径:"+path);
for(int i=;i<image.length;i++){
FileUtils.copyFile(image[i], new File(path,imageFileName[i]));
System.out.println("文件类型:"+imageContentType[i]);
}
}
ActionContext.getContext().put("message", "文件上传成功");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
} } }
<?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.devMode" value="true"></constant>
<constant name="struts.action.extension" value="do,,action"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 全局布置 -->
<package name="mypackage" extends="struts-default">
<interceptors><!-- 全局拦截器 。拦截器必须在最前-->
<interceptor name="loginInterceptor" class="cn.gs.ly.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="mydefaultInterceptor">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors> <global-results><!-- 全局错误页 -->
<result name="error" type="dispatcher">/customer/error.jsp</result>
</global-results>
</package> <package name="webObj" namespace="/webObj" extends="mypackage">
<action name="webAction" class="cn.gs.ly.webAction" method="execute1">
<result type="dispatcher" name="success">web.jsp</result>
</action>
<action name="webAction1" class="cn.gs.ly.webAction" method="execute2">
<result type="dispatcher" name="success">web.jsp</result>
</action>
</package> <!--单文件上传upload。多文件上传upload2. -->
<package name="upload" namespace="/upload" extends="mypackage">
<action name="upload1" class="cn.gs.ly.UploadAction" method="execute1">
<result type="dispatcher" name="success">/success.jsp</result>
<result type="dispatcher" name="input">/upload1.jsp</result>
</action>
<action name="upload2" class="cn.gs.ly.UploadAction2" method="execute2">
<result type="dispatcher" name="success">/success.jsp</result>
<result type="dispatcher" name="input">/upload2.jsp</result>
</action>
</package> <package name="interceptor" namespace="/interceptor" extends="mypackage">
<action name="visitAction" class="cn.gs.ly.UserAction" >
<!--引用全局拦截器 -->
<interceptor-ref name="mydefaultInterceptor"></interceptor-ref> <result type="dispatcher" name="success">/index.jsp</result>
<result type="dispatcher" name="login">/login.jsp</result>
</action>
</package> </struts>
内置拦截器
自定义拦截器:
1、编写一个类,实现 com.opensymphony.xwork2.interceptor.Interceptor
2.编写代码逻辑 ,实现Interceptor类 public String intercept(ActionInvocation invocation) throws Exception {}方法
package cn.gs.ly.interceptor; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor; public class LoginInterceptor implements Interceptor{ @Override
public void destroy() {
// TODO Auto-generated method stub } @Override
public void init() {
// TODO Auto-generated method stub } @Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("进入拦截器");
HttpSession session = ServletActionContext.getRequest().getSession();
Object obj = session.getAttribute("user");
if(obj==null){
return "login";
}else{
return invocation.invoke(); //调用动作方法
}
} }
3、注册拦截器。拦截器定义好后,一定要在配置文件中进行注册:
<interceptors><!-- 只是定义拦截器,并没有起作用-->
<interceptor name="loginInterceptor" class="cn.gs.ly.interceptor.LoginInterceptor"></interceptor>
</interceptors>
4、配置文件中的动作,要通过
1 <interceptor-ref name="loginInterceptor"></interceptor-ref> <!-- 不采取 -->
注意:一旦动作中使用了自定义的拦截器,那么默认的就不起作用了。一般应该采用如下的做法:
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginInterceptor"></interceptor-ref>
多个动作类都要使用的话,可以通过package来进行组合。
<interceptors><!-- 全局拦截器 。拦截器必须在错误页等最前-->
<interceptor name="loginInterceptor" class="cn.gs.ly.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="mydefaultInterceptor">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 使用时引用全局拦截器 -->
<interceptor-ref name="mydefaultInterceptor"></interceptor-ref>
完整配置: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.devMode" value="true"></constant>
<constant name="struts.action.extension" value="do,,action"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 全局布置 -->
<package name="mypackage" extends="struts-default">
<interceptors><!-- 全局拦截器 。拦截器必须在错误页等最前-->
<interceptor name="loginInterceptor" class="cn.gs.ly.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="mydefaultInterceptor">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors> <global-results><!-- 全局错误页 -->
<result name="error" type="dispatcher">/customer/error.jsp</result>
</global-results>
</package> <package name="webObj" namespace="/webObj" extends="mypackage">
<action name="webAction" class="cn.gs.ly.webAction" method="execute1">
<result type="dispatcher" name="success">web.jsp</result>
</action>
<action name="webAction1" class="cn.gs.ly.webAction" method="execute2">
<result type="dispatcher" name="success">web.jsp</result>
</action>
</package> <!--单文件上传upload。多文件上传upload2. -->
<package name="upload" namespace="/upload" extends="mypackage">
<action name="upload1" class="cn.gs.ly.UploadAction" method="execute1">
<result type="dispatcher" name="success">/success.jsp</result>
<result type="dispatcher" name="input">/upload1.jsp</result>
</action>
<action name="upload2" class="cn.gs.ly.UploadAction2" method="execute2">
<result type="dispatcher" name="success">/success.jsp</result>
<result type="dispatcher" name="input">/upload2.jsp</result>
</action>
</package> <package name="interceptor" namespace="/interceptor" extends="mypackage">
<action name="visitAction" class="cn.gs.ly.UserAction" >
<!--引用全局拦截器 -->
<interceptor-ref name="mydefaultInterceptor"></interceptor-ref> <result type="dispatcher" name="success">/index.jsp</result>
<result type="dispatcher" name="login">/login.jsp</result>
</action>
</package> </struts>
struts2 基础3 文件上传、拦截器的更多相关文章
- struts文件上传拦截器分析
struts有默认的文件拦截器,一般配置maximumSize就可以了. 知道原理,我们可以写一个类继承它,实现自己的配置上传文件大小的方式. 然后细究页面上传文件的时候,发现了一些问题. act ...
- SpringMVC 文件上传&拦截器&异常处理
文件上传 Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的.Spring 用 Jakarta Commons FileUpload ...
- struts文件上传拦截器中参数的配置(maximumSize,allowedTypes ,allowedExtensions)问题
<interceptor-ref name="fileUpload"> <param name="allowedTypes">image ...
- struts文件上传拦截器maximumSize设置文件大小不起作用
<interceptor-ref name="fileUpload"> <param name="allowedTypes ...
- struts2中的文件上传,文件下载
文件上传: Servlet中的文件上传回顾 前台页面 1.提交方式post 2.表单类型 multipart/form-data 3.input type=file 表单输入项 后台 apache提交 ...
- struts2之多文件上传与拦截器(8)
前台jsp <s:form action="uploadAction" enctype="multipart/form-data" method=&quo ...
- [转]Struts2多个文件上传
转载至:http://blog.csdn.net/hanxiaoshuang123/article/details/7342091 Struts2多个文件上传多个文件上传分为List集合和数组,下面我 ...
- struts2之单文件上传(7)
前台页面jsp <!-- 拦截的时候用这个 <s:form action="uploadAction" enctype="multipart/form-dat ...
- springMVC整理04--文件上传 & 拦截器 & 异常处理
1. 文件上传 SpringMVC 的文件上传非常简便,首先导入文件上传依赖的 jar: <!-- 文件上传所依赖的 jar 包 --> <dependency> <g ...
随机推荐
- 第一次整合ssm环境后,对请求流程的理解 ,以及一些配置(有错就更新)
工程结构图: 显示层(handler/controller): request请求到springmvc的前端控制器,从处理器映射器找相应的handler(用@RequestMapping(" ...
- MongoDB入门_MongoDB安装与配置
MongoDB运行环境 MongoDB环境:CentOS-6.7-i386 MongoDB版本:MongoDB 2.6.5 ssh工具:xshell 文本编辑工具:vim与editplus++ 编译M ...
- Django发送邮件功能
以126邮箱为例 1 首先进126邮箱设置,开启: POP3/SMTP服务 IMAP/SMTP服务 成功开启后会获得一个授权码. 2. setting.py配置: # 配置发送邮箱 # 需要登录网 ...
- PAT Advanced 1065 A+B and C (64bit) (20 分)(关于g++和clang++修改后能使用)
Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...
- 05java基础
1.BigInteger和BigDecimal类 package cn.jxufe.java.chapter5.demo01; import java.math.BigInteger; public ...
- Kettle整理
下载kettle版本 (1)hadoop version 查看hadoop的版本 hadoop2.6 (2)则在data-integration\plugins\pentaho-big-data ...
- 洛谷 P4665 [BalticOI 2015]Network
洛谷 P4665 [BalticOI 2015]Network 你有一棵 $ n $ 个节点的树,你可以在树上加一些边,使这棵树变成一张无重边.自环的图,且删掉任意一条边它仍然联通.求最少要加多少条边 ...
- python-类对象的遍历操作
视频教程 https://study.163.com/course/courseLearn.htm?courseId=1005985001#/learn/video?lessonId=10533511 ...
- Android App学习计划
模块化 Json Gson Fastjson Jackson EventBus GreenDao Flutter ButterKnife Dagger okhttp Rxjava/Rxandroid ...
- css3 中的2D转换
一.CSS3转换 通过转换实现对对元素进行旋转.缩放.移动.拉伸的效果:这种原来必须要通过JS或者图片处理才可以实现的效果,现在都可以通过CSS3来完成. 2D转换采用transform属性来实现效果 ...