文件上传:

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 文件上传、拦截器的更多相关文章

  1. struts文件上传拦截器分析

    struts有默认的文件拦截器,一般配置maximumSize就可以了. 知道原理,我们可以写一个类继承它,实现自己的配置上传文件大小的方式.   然后细究页面上传文件的时候,发现了一些问题. act ...

  2. SpringMVC 文件上传&拦截器&异常处理

    文件上传 Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的.Spring 用 Jakarta Commons FileUpload ...

  3. struts文件上传拦截器中参数的配置(maximumSize,allowedTypes ,allowedExtensions)问题

    <interceptor-ref name="fileUpload"> <param name="allowedTypes">image ...

  4. struts文件上传拦截器maximumSize设置文件大小不起作用

    <interceptor-ref name="fileUpload">                <param name="allowedTypes ...

  5. struts2中的文件上传,文件下载

    文件上传: Servlet中的文件上传回顾 前台页面 1.提交方式post 2.表单类型 multipart/form-data 3.input type=file 表单输入项 后台 apache提交 ...

  6. struts2之多文件上传与拦截器(8)

    前台jsp <s:form action="uploadAction" enctype="multipart/form-data" method=&quo ...

  7. [转]Struts2多个文件上传

    转载至:http://blog.csdn.net/hanxiaoshuang123/article/details/7342091 Struts2多个文件上传多个文件上传分为List集合和数组,下面我 ...

  8. struts2之单文件上传(7)

    前台页面jsp <!-- 拦截的时候用这个 <s:form action="uploadAction" enctype="multipart/form-dat ...

  9. springMVC整理04--文件上传 & 拦截器 & 异常处理

    1.  文件上传 SpringMVC 的文件上传非常简便,首先导入文件上传依赖的 jar: <!-- 文件上传所依赖的 jar 包 --> <dependency> <g ...

随机推荐

  1. empty()、isset()、is_null()的区别

    总结:1. 变量有二种状态: 已声明, 未声明2. 已声明的变量也有二种状态: 已赋值(初始化), 未赋值(未初始化)3. 变量可能会被赋值类型: null, 空值, 非空值 3.1: null值: ...

  2. 关于阅读Struts2部分拦截器源码的记录

    Struts2中的拦截器在ActionInvocation对象的invoke()方法中执行. ActionInvocation对象从配置文件中读取Interceptor对象,加入到自己的存取拦截器的容 ...

  3. html常见标签及用法整理

    <!DOCTYPE html> <!--#浏览器的兼容模式--> <html lang="en"> <head> <!--he ...

  4. IDEA更改左侧目录层级结构

    齿轮---Compact Empty Middle Packages

  5. JAVA(-Xms,Xmx,Xmn-XX:newSize,-XX:MaxnewSize,-XX:PermSize,-XX:MaxPermSize)区别

    1.-Xms:表示java虚拟机堆区内存初始内存分配的大小,通常为操作系统可用内存的1/64大小即可,但仍需按照实际情况进行分配.2.-Xmx:表示java虚拟机堆区内存可被分配的最大上限,通常为操作 ...

  6. Maven高级

    第一章 Maven解决冲突的方式 1.1 第一声明者优先原则 那个jar包的坐标在pom.xml文件上属于靠上的位置,这个jar包就是先声明的.先声明的jar包坐标下的依赖包,可以优先进入项目中. 示 ...

  7. eclipse中export 的jar file与 runnable jar file的区别

    (1)直接运行 .class的方法     java  -cp .  com.guangfa.demo1  , 不用加.class后缀 .-cp 是 -classpath 缩写,是指定类运行所依赖其他 ...

  8. python-类对象的遍历操作

    视频教程 https://study.163.com/course/courseLearn.htm?courseId=1005985001#/learn/video?lessonId=10533511 ...

  9. vue 报错 :属性undefined(页面成功渲染)

    vue 报错:Cannot read property 'instrumentId' of undefined" 相关代码如下: <template> ... <span& ...

  10. Python---进阶---文件操作---按需求打印文件的内容

    一. 编写一个程序,当用户输入文件名和行数的时候,将该文件的前N行内容打印到屏幕上 input 去接收一个文件名 input 去接收一个行数 ----------------------------- ...