文件上传:
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>