struts2攔截器與檔案上傳、下載
阿新 • • 發佈:2018-11-28
1、攔截器
定義一個action。.
TestAction.java
package com.zking.study.five;
/**
* 用於測試的action,與普通action沒啥區別
*/
public class TestAction{
public String execute() {
System.out.println("進入了InterceptorAction的execute方法。");
return null;
}
}
定義一個攔截器,實現Interceptor介面,( com.opensymphony.xwork2.interceptor.Interceptor )
TestInterceptor.java
package com.zking.study.five; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; /** * 用於測試的攔截器,實現Interceptor介面 */ public class TestInterceptor implements Interceptor{ @Override public void destroy() { System.out.println("TestInterceptor is destroying."); } @Override public void init() { System.out.println("TestInterceptor is initing."); } @Override public String intercept(ActionInvocation arg0) throws Exception { System.out.println("攔截的類:"+arg0.getAction().getClass().getName()); System.out.println("呼叫的Action是:"+arg0.getProxy().getActionName()); System.out.println("呼叫的方法是:"+arg0.getProxy().getMethod()); arg0.invoke(); return null; } }
struts中的配置檔案 struts-sy.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="sy" extends="base" namespace="/sy"> <!-- 攔截器定義在package標籤下,於action同級 --> <interceptors> <interceptor name="testInterceptor" class="com.zking.study.five.TestInterceptor"></interceptor> </interceptors> <!-- 配置我們之前寫的那個測試action --> <action name="testAction" class="com.zking.study.five.TestAction"> <!-- 將之前定義好的攔截器配置到action裡,(可配置多個) --> <interceptor-ref name="testInterceptor"></interceptor-ref> </action> </package> </struts>
在上面第二段程式碼塊TestInterceptor.java中,destroy方法為攔截器被銷燬時呼叫的方法、init方法為攔截器初始化時呼叫的方法,前面兩個方法都沒什麼好說的,最後一個方法以及它的引數才是重點。
intercept(ActionInvocation arg0) ,在這裡就不多解釋了: https://blog.csdn.net/beliefyou8/article/details/51956279
2、檔案上傳、下載
在下面的demo中是用到了一個jar包的 commons-io-2.5.jar,使用前需要先將jar包匯入進去,不導jar也行,需自己寫檔案操作的程式碼。
<!-- struts中的s標籤 -->
<s:form action="fileAction_upload" namespace="/sy" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</s:form>
定義上傳、下載、開啟檔案的類
package com.zking.study.five;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.zking.study.four.base.BaseAction;
/**
* 檔案上傳下載
*/
public class FileAction extends BaseAction{
//這是jsp頁面傳遞過來的具體檔案
private File file;
//檔名
private String fileFileName;
//檔案型別
private String fileContentType;
//虛擬路徑
private String serverDir="/upload";
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
//上傳圖片到tomcat
public String upload() {
String realPath = getRealPath(serverDir+"/"+fileFileName);
try {
FileUtils.copyFile(file, new File(realPath));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
//得到伺服器中的真實路徑
private String getRealPath(String path) {
return this.request.getServletContext().getRealPath(path);
}
//儲存圖片
public String saveAs() {
String fileName="stationmaster_img.png";
String fileType="image/png";
this.response.setContentType(fileType);
this.response.setHeader("Content-Disposition","attachment;filename=" + fileName);
String realPath=getRealPath(serverDir+"/"+fileName);
try {
//commons-io-2.5.jar 中的方法,不使用該方法則需自己寫檔案操作的方法。
FileUtils.copyFile(new File(realPath), response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//開啟圖片
public String openAs() {
String fileName="stationmaster_img.png";
String fileType="image/png";
this.response.setContentType(fileType);
this.response.setHeader("Content-Disposition","filename=" + fileName);
String realPath=getRealPath(serverDir+"/"+fileName);
try {
FileUtils.copyFile(new File(realPath), response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
BaseAction.java
package com.zking.study.four.base;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
public class BaseAction implements ServletRequestAware, ServletResponseAware{
/**
* 為了傳值使用
*/
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
protected ServletContext application;
/**
* 為了配置跳轉頁面所用
*/
protected final static String SUCCESS="success";
protected final static String FAIL="fail";
protected final static String LIST="list";
protected final static String ADD="add";
protected final static String PROADD="proAdd";
protected final static String EDIT="edit";
protected final static String PROEDIT="proEdit";
protected final static String DEL="del";
protected final static String TOLIST="tolist";
protected final static String DETAIL="detail";
/**
* 具體傳值欄位 後端向jsp頁面傳值所用欄位
*/
protected Object result;
protected Object msg;
protected int code;
public Object getResult() {
return result;
}
public Object getMsg() {
return msg;
}
public int getCode() {
return code;
}
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request=request;
}
}
點選上傳後跳轉的頁面
<body>
<img src="${pageContext.request.contextPath}/sy/fileAction_openAs.action">
<a href="${pageContext.request.contextPath}/sy/fileAction_saveAs.action">點選下載</a>
</body>
struts2配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<action name="fileAction_*" class="com.zking.study.five.FileAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
上傳檔案是有兩個概念需要理解的,虛擬路徑與真實路徑的概念?
相對於你所在的開發電腦而言,你自己電腦上有的就是真實路徑,你電腦上沒有的路徑就是虛擬的。