文件上传:
1、将头设置为enctype=”multipart/form-data”
1 <form action="${pageContext.request.contextPath }/upload/upload1" method="post" enctype="multipart/form-data"> 2 文件<input type="file" name="image"><br> 3 <input type="submit" value="上传"> 4 </form>
2、写接收处理的方法,有两种,一种是自己实现IO流,一种是使用FileUtils
1 package cn.gs.ly; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 10 import org.apache.commons.io.FileUtils; 11 import org.apache.struts2.ServletActionContext; 12 13 import com.opensymphony.xwork2.ActionContext; 14 import com.opensymphony.xwork2.ActionSupport; 15 16 public class UploadAction extends ActionSupport { 17 private File image; //struts2封装好的 上传的文件 18 private String imageFileName; //上传输入域的文件名 19 private String imageContentType; //上传文件的MIME类型 20 21 //setter/getter方法 22 public String getImageContentType() { 23 return imageContentType; 24 } 25 public void setImageContentType(String imageContentType) { 26 this.imageContentType = imageContentType; 27 } 28 public String getImageFileName() { 29 return imageFileName; 30 } 31 public void setImageFileName(String imageFileName) { 32 this.imageFileName = imageFileName; 33 } 34 public File getImage() { 35 return image; 36 } 37 public void setImage(File image) { 38 this.image = image; 39 } 40 41 42 public String execute1(){ 43 System.out.println("文件类型:"+imageContentType); 44 //文件存放的真实路径 45 String path = ServletActionContext.getServletContext().getRealPath("files"); 46 System.out.println("文件存放路径:"+path); 47 File f = new File(path); 48 if(!f.exists()){ 49 f.mkdirs(); //创建一个路径 50 } 51 52 try { 53 // //自己实现IO流 。构建输入输出流 54 // InputStream in = new FileInputStream(image); 55 // OutputStream out = new FileOutputStream(f+"\"+imageFileName); 56 // byte b[] = new byte[1024]; 57 // int len=-1; 58 // while((len=in.read(b))!=-1){ 59 // out.write(b, 0, len); 60 // } 61 // out.close(); 62 // in.close(); 63 64 //使用FileUtils 65 FileUtils.copyFile(image, new File(path,imageFileName)); 66 67 ActionContext.getContext().put("message", "文件上传成功"); 68 return SUCCESS; 69 } catch (Exception e) { 70 e.printStackTrace(); 71 return ERROR; 72 } 73 74 } 75 }
多文件上传:
1 package cn.gs.ly; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 10 import org.apache.commons.io.FileUtils; 11 import org.apache.struts2.ServletActionContext; 12 13 import com.opensymphony.xwork2.ActionContext; 14 import com.opensymphony.xwork2.ActionSupport; 15 16 public class UploadAction2 extends ActionSupport { 17 private File[] image; //struts2封装好的 上传的多个文件数组 18 private String[] imageFileName; //上传输入域的文件名 19 private String[] imageContentType; //上传文件的MIME类型 20 21 //setter/getter方法 22 public File[] getImage() { 23 return image; 24 } 25 public void setImage(File[] image) { 26 this.image = image; 27 } 28 public String[] getImageFileName() { 29 return imageFileName; 30 } 31 public void setImageFileName(String[] imageFileName) { 32 this.imageFileName = imageFileName; 33 } 34 public String[] getImageContentType() { 35 return imageContentType; 36 } 37 public void setImageContentType(String[] imageContentType) { 38 this.imageContentType = imageContentType; 39 } 40 41 42 public String execute2(){ 43 try { 44 if(image!=null&&image.length>0){ 45 46 //文件存放的真实路径 47 String path = ServletActionContext.getServletContext().getRealPath("files"); 48 System.out.println("文件存放路径:"+path); 49 for(int i=0;i<image.length;i++){ 50 FileUtils.copyFile(image[i], new File(path,imageFileName[i])); 51 System.out.println("文件类型:"+imageContentType[i]); 52 } 53 } 54 ActionContext.getContext().put("message", "文件上传成功"); 55 return SUCCESS; 56 } catch (Exception e) { 57 e.printStackTrace(); 58 return ERROR; 59 } 60 61 } 62 63 64 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true"></constant> 7 <constant name="struts.action.extension" value="do,,action"></constant> 8 <constant name="struts.i18n.encoding" value="UTF-8"></constant> 9 10 <!-- 全局布置 --> 11 <package name="mypackage" extends="struts-default"> 12 <interceptors><!-- 全局拦截器 。拦截器必须在最前--> 13 <interceptor name="loginInterceptor" class="cn.gs.ly.interceptor.LoginInterceptor"></interceptor> 14 <interceptor-stack name="mydefaultInterceptor"> 15 <interceptor-ref name="defaultStack"></interceptor-ref> 16 <interceptor-ref name="loginInterceptor"></interceptor-ref> 17 </interceptor-stack> 18 </interceptors> 19 20 <global-results><!-- 全局错误页 --> 21 <result name="error" type="dispatcher">/customer/error.jsp</result> 22 </global-results> 23 </package> 24 25 <package name="webObj" namespace="/webObj" extends="mypackage"> 26 <action name="webAction" class="cn.gs.ly.webAction" method="execute1"> 27 <result type="dispatcher" name="success">web.jsp</result> 28 </action> 29 <action name="webAction1" class="cn.gs.ly.webAction" method="execute2"> 30 <result type="dispatcher" name="success">web.jsp</result> 31 </action> 32 </package> 33 34 <!--单文件上传upload。多文件上传upload2. --> 35 <package name="upload" namespace="/upload" extends="mypackage"> 36 <action name="upload1" class="cn.gs.ly.UploadAction" method="execute1"> 37 <result type="dispatcher" name="success">/success.jsp</result> 38 <result type="dispatcher" name="input">/upload1.jsp</result> 39 </action> 40 <action name="upload2" class="cn.gs.ly.UploadAction2" method="execute2"> 41 <result type="dispatcher" name="success">/success.jsp</result> 42 <result type="dispatcher" name="input">/upload2.jsp</result> 43 </action> 44 </package> 45 46 <package name="interceptor" namespace="/interceptor" extends="mypackage"> 47 <action name="visitAction" class="cn.gs.ly.UserAction" > 48 <!--引用全局拦截器 --> 49 <interceptor-ref name="mydefaultInterceptor"></interceptor-ref> 50 51 <result type="dispatcher" name="success">/index.jsp</result> 52 <result type="dispatcher" name="login">/login.jsp</result> 53 </action> 54 </package> 55 56 </struts>
内置拦截器
自定义拦截器:
1、编写一个类,实现 com.opensymphony.xwork2.interceptor.Interceptor
2.编写代码逻辑 ,实现Interceptor类 public String intercept(ActionInvocation invocation) throws Exception {}方法
1 package cn.gs.ly.interceptor; 2 3 import javax.servlet.http.HttpSession; 4 5 import org.apache.struts2.ServletActionContext; 6 7 import com.opensymphony.xwork2.ActionInvocation; 8 import com.opensymphony.xwork2.interceptor.Interceptor; 9 10 public class LoginInterceptor implements Interceptor{ 11 12 @Override 13 public void destroy() { 14 // TODO Auto-generated method stub 15 16 } 17 18 @Override 19 public void init() { 20 // TODO Auto-generated method stub 21 22 } 23 24 @Override 25 public String intercept(ActionInvocation invocation) throws Exception { 26 System.out.println("进入拦截器"); 27 HttpSession session = ServletActionContext.getRequest().getSession(); 28 Object obj = session.getAttribute("user"); 29 if(obj==null){ 30 return "login"; 31 }else{ 32 return invocation.invoke(); //调用动作方法 33 } 34 } 35 36 }
3、注册拦截器。拦截器定义好后,一定要在配置文件中进行注册:
1 <interceptors><!-- 只是定义拦截器,并没有起作用--> 2 <interceptor name="loginInterceptor" class="cn.gs.ly.interceptor.LoginInterceptor"></interceptor> 3 </interceptors>
4、配置文件中的动作,要通过
1 <interceptor-ref name="loginInterceptor"></interceptor-ref> <!-- 不采取 -->
注意:一旦动作中使用了自定义的拦截器,那么默认的就不起作用了。一般应该采用如下的做法:
1 <interceptor-ref name="defaultStack"></interceptor-ref> 2 <interceptor-ref name="loginInterceptor"></interceptor-ref>
多个动作类都要使用的话,可以通过package来进行组合。
1 <interceptors><!-- 全局拦截器 。拦截器必须在错误页等最前--> 2 <interceptor name="loginInterceptor" class="cn.gs.ly.interceptor.LoginInterceptor"></interceptor> 3 <interceptor-stack name="mydefaultInterceptor"> 4 <interceptor-ref name="defaultStack"></interceptor-ref> 5 <interceptor-ref name="loginInterceptor"></interceptor-ref> 6 </interceptor-stack> 7 </interceptors>
1 <!-- 使用时引用全局拦截器 --> 2 <interceptor-ref name="mydefaultInterceptor"></interceptor-ref>
完整配置:struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true"></constant> 7 <constant name="struts.action.extension" value="do,,action"></constant> 8 <constant name="struts.i18n.encoding" value="UTF-8"></constant> 9 10 <!-- 全局布置 --> 11 <package name="mypackage" extends="struts-default"> 12 <interceptors><!-- 全局拦截器 。拦截器必须在错误页等最前--> 13 <interceptor name="loginInterceptor" class="cn.gs.ly.interceptor.LoginInterceptor"></interceptor> 14 <interceptor-stack name="mydefaultInterceptor"> 15 <interceptor-ref name="defaultStack"></interceptor-ref> 16 <interceptor-ref name="loginInterceptor"></interceptor-ref> 17 </interceptor-stack> 18 </interceptors> 19 20 <global-results><!-- 全局错误页 --> 21 <result name="error" type="dispatcher">/customer/error.jsp</result> 22 </global-results> 23 </package> 24 25 <package name="webObj" namespace="/webObj" extends="mypackage"> 26 <action name="webAction" class="cn.gs.ly.webAction" method="execute1"> 27 <result type="dispatcher" name="success">web.jsp</result> 28 </action> 29 <action name="webAction1" class="cn.gs.ly.webAction" method="execute2"> 30 <result type="dispatcher" name="success">web.jsp</result> 31 </action> 32 </package> 33 34 <!--单文件上传upload。多文件上传upload2. --> 35 <package name="upload" namespace="/upload" extends="mypackage"> 36 <action name="upload1" class="cn.gs.ly.UploadAction" method="execute1"> 37 <result type="dispatcher" name="success">/success.jsp</result> 38 <result type="dispatcher" name="input">/upload1.jsp</result> 39 </action> 40 <action name="upload2" class="cn.gs.ly.UploadAction2" method="execute2"> 41 <result type="dispatcher" name="success">/success.jsp</result> 42 <result type="dispatcher" name="input">/upload2.jsp</result> 43 </action> 44 </package> 45 46 <package name="interceptor" namespace="/interceptor" extends="mypackage"> 47 <action name="visitAction" class="cn.gs.ly.UserAction" > 48 <!--引用全局拦截器 --> 49 <interceptor-ref name="mydefaultInterceptor"></interceptor-ref> 50 51 <result type="dispatcher" name="success">/index.jsp</result> 52 <result type="dispatcher" name="login">/login.jsp</result> 53 </action> 54 </package> 55 56 </struts>