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 ...
随机推荐
- 前端开发HTML&css入门——伪类选择器和一些特殊的选择器
伪类和伪元素 有时候,你需要选择本身没有标签,但是仍然易于识别的网页部位,比如段落首行或鼠标滑过的连接.CSS为他们提供一些选择器:伪类和伪元素. 常用的一些伪类选择器: :link :visited ...
- slots_doc_call属性
class Foo: "这时文档doc属性" __slots__ = ["name","age"] #[“name”=None,“age&q ...
- 个人第二次作业-c++实现四则运算生成器
c++实现四则运算生成器 GIT地址 Link Git用户名 Redwarx008 学号后五位 61128 博客地址 Link 作业链接 Link 环境配置 使用VS2019社区版,一键式安装,这里不 ...
- Linux之bash的变量
1. 变量的显示,echo echo $变量 或 echo ${变量} eg. echo $HOME 或 echo ${HOME} 2. 变量的设置 变量的设置规则: (1)变量与变 ...
- hdu 1505 单调栈升级版
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...
- font-awesome样式只显示方框
这是一个踩过的坑:使用font-awesome中的css样式库时,比如fa-user-circle-o,显示的不是一个用户图标,而是一个方框. 怎么回事呢? 进入css文件,发现: 咦,这些文件呢?我 ...
- SpringCloud学习系列-微服务
最近和尚硅谷周阳老师学习了Spring Cloud感觉有必要在这里做下笔记和总结. 软件系统架构演变 单一应用架构当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本.此时,用 ...
- ASCII 、UTF-8、Unicode编码
1.各种编码的由来 1.1.计算机编码的由来 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.所以只能是用一些数字来表示文本,这就是编码的由来.最早的计算机在设计时采用8个比 ...
- Ubuntu 16.04下使用docker部署MySQL主从复制
(以下docker相关的命令,需要在root用户环境下或通过sudo提升权限来进行操作.) 首先更新 软件源 https://mirrors.tuna.tsinghua.edu.cn/help/ubu ...
- Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)
3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the long ...