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 ...
随机推荐
- [七月挑选]使用idea创建spring boot 项目
title: 使用idea创建spring boot 项目 参考lindaZ的IntelliJ IDEA 创建spring boot 的Hello World 项目 1.Open IDEA,choos ...
- make_smbcodepage - 为Samba创建代码页文件
总览 make_smbcodepage c|d 代码页 输入文件 输出文件 描述 这个工具是是Samba组件的一部分. 针对Samba 2.2的国际化功能,使用make_smbcodepage可以编译 ...
- Cookie、Session和Django分页
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
- Vim 系列笔记一
Vim 系列笔记一 Vim 简介 什么是VIM ? Vim 是从 Vi 发展出来的一个编辑器,是 Vi 的升级版.而 vi 则是 Unix .类Unix(Linux)系统中自带的编辑器. Vim/Vi ...
- VB.NET Event RaiseEvent用处
一.代码 Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As System.EventA ...
- Keepalived+Nginx+tomcat实现主备+负载
部署系统: Red Hat Enterprise Linux Server release 7.0 软件版本:apache-tomcat-7.0.92.tar.gzkeepalived-2.0.11. ...
- Linux下Centos7对外开放端口
转载:https://blog.csdn.net/realjh/article/details/82048492 命令集合: ()查看对外开放的端口状态 查询已开放的端口 netstat -anp 查 ...
- MySQL集群安装与配置
MySQL集群安装与配置 文章目录 [隐藏] 一.mysql集群安装 二.节点配置 三.首次启动节点 四.测试服务是否正常 五.安全关闭和重启 MySQL Cluster 是 MySQL 适合于分 ...
- 半小时写完替罪羊重构点分树做动态动态点分治之紫荆花之恋的wyy贴心指导
刷题训练 初学者 有一定语言基础,但是不了解算法竞赛,水平在联赛一等奖以下的. 参考书:<算法竞赛入门经典--刘汝佳>,<算法竞赛入门经典训练指南--刘汝佳> 题库:洛谷(历年 ...
- day02记
一.Tomcat搭建 1.service和controller分别搭建独立的Tomcat且port不一致 2.部署项目选择带有exploded的 3.运行时应先启动service再启动controll ...