十九、文件上傳三部曲
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>My JSP 'employeeAdd.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

</head>

<body>

<form action="${pageContext.request.contextPath}/control/employee/list_execute.action" enctype="multipart/form-data" method="post">

文件:<input type="file" name="image">

<input type="submit" value="上傳"/>

</form>

</body>

</html>
package cn.itcast.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class HelloWorldAction {

private File[] image;

private String[] imageFileName;

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 addUI(){

return "success";

}

public String execute() throws Exception{

String realpath = ServletActionContext.getServletContext().getRealPath("/images");

System.out.println(realpath);

if(image!=null){

File savedir = new File(realpath);

if(!savedir.exists()) savedir.mkdirs();

for(int i = 0 ; i<image.length ; i++){

File savefile = new File(savedir, imageFileName[i]);

FileUtils.copyFile(image[i], savefile);

}

ActionContext.getContext().put("message", "上傳成功");

}

return "success";

}

}

package cn.itcast.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class PermissionInterceptor implements Interceptor {

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

Object user = ActionContext.getContext().getSession().get("user");

if(user!=null) return invocation.invoke();
//如果user不為null,代表用戶已經登錄,允許執行action中的方法

ActionContext.getContext().put("message", "你沒有權限執行該操作");

return "success";
}
}
<package name="employee" namespace="/control/employee" extends="struts-default">

<interceptors>

<interceptor name="permission" class="cn.itcast.interceptor.PermissionInterceptor"/>

<interceptor-stack name="permissionStack">

<interceptor-ref name="defaultStack"/>

<interceptor-ref name="permission" />

</interceptor-stack>

</interceptors>

<global-results>

<result name="success">/WEB-INF/page/message.jsp</result>

</global-results>

<action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}">

<interceptor-ref name="permissionStack" />

</action>

</package>
第一步、jar文件的準備
commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar
第二步、把form表的enctype設置為:multipart/form-data
































第三步、在Action類中添加以下屬性
注意:
可以設置Struts2的常量struts.multipart.maxSize來設置上傳文件大小
可以得到上傳文件類型
web上傳文件大小注意不要太大,一般的視頻網站上傳大文件是通過通訊軟件上傳的,即socket通訊
多文件上傳:













































































二十、自定義攔截器
注意:
1.自定義攔截器的部署時候需要定義攔截器棧,在該棧中需要引入系統默認的攔截器,如果直接應用則會導致系統所有的攔截器對該action都會失效
2.所以要注意攔截器的應用范圍:action/package
3.每個包只能指定一個默認的攔截器
<default-interceptor-ref name=”permissionStack” />
4.一個action可以定義多個攔截器
<interceptor-ref name=”interceptor1” />
<interceptor-ref name=”interceptor2” />
攔截器:





























部署:





























