Struts之攔截器與檔案上傳下載
一、攔截器(Interceptor)
攔截器是動態攔截Action呼叫的物件,攔截器將Action共用的行為獨立出來,在Action執行前後執行。這也就是我們所說的AOP,它是分散關注的程式設計方法,它將通用需求功能從不相關類之中分離出來;同時,能夠共享一個行為,一旦行為發生變化,不必修改很多類,只要修改這個行為就可以。
寫action方法要繼承Interceptor,要有三個方法
public class OneInterceptor implements Interceptor{ /** * */ private static final long serialVersionUID = 1L; @Override public void destroy() {//銷燬 關閉伺服器是執行 // TODO Auto-generated method stub } @Override public void init() {//初始化 開啟伺服器是執行 // TODO Auto-generated method stub System.out.println("OneInterceptor=======init"); } @Override public String intercept(ActionInvocation arg0) throws Exception { System.out.println("OneInterceptor=======intercept"); return arg0.invoke();//要是上面的方法通過,則放行 } }
在Struts.sy.xml配置時,要配置interceptor
<interceptors> <interceptorname="oneInter"class="com.zking.struts.four.OneInterceptor"></interceptor> <interceptorname="twonter"class="com.zking.struts.four.TwoInterceptor"></interceptor> </interceptors> <action name="interAction" class="com.zking.struts.four.InterceptorAction" method="{1}"> <interceptor-ref name="oneInter"></interceptor-ref> <interceptor-ref name="twonter"></interceptor-ref> </action>
過濾器(Filter)
Servlet中的過濾器Filter是實現了javax.servlet.Filter介面的伺服器端程式,主要的用途是設定字符集、控制權限、控制轉向、做一些業務邏輯判斷等。其工作原理是,只要你在web.xml檔案配置好要攔截的客戶端請求,它都會幫你攔截到請求,此時你就可以對請求或響應(Request、Response)統一設定編碼,簡化操作;同時還可進行邏輯判斷,如使用者是否已經登陸、有沒有許可權訪問該頁面等等工作。它是隨你的web應用啟動而啟動的,只初始化一次,以後就可以攔截相關請求,只有當你的web應用停止或重新部署的時候才銷燬。
攔截器(Interceptor)和過濾器(Filter)的區別
Filter | Interceptor | |
Filter 介面定義在 javax.servlet 包中 | 介面 HandlerInterceptor 定義在org.springframework.web.servlet 包中 | |
Filter 定義在 web.xml 中 | Struts.sy.xml配置 | |
Filter 是 Servlet 規範規定的。 | 而攔截器既可以用於Web程式,也可以用於Application、Swing程式中。 | |
Filter 是被 Server(like Tomcat) 呼叫 |
|
攔截器(Interceptor)和過濾器(Filter)的執行順序
過濾前-攔截前-Action處理-攔截後-過濾後 ---
先到web.xml中進行過濾,然後在去攔截(先Filter,後Interceptor)
二、檔案上傳
1、檔案上傳的三種方案
①將上傳的檔案存到資料庫(適合oa系統 activity工作流框架)
②上傳檔案到tomcat普通的web伺服器
③上傳檔案到檔案伺服器中(磁碟足夠大)
2、真實路徑和虛擬路徑的概念 真實路徑:能夠在自己的電腦上看得到 虛擬路徑:在自個電腦上看不到,在別人的電腦上看的到
那麼下面就真空上程式碼吧
<h1>strut檔案上傳</h1> <form action="${pageContext.request.contextPath }/sy/uploadAction_upload.action" enctype="multipart/form-data" method="post"> <input type="file" name="file"/><!--注意這個name屬性的file--> <input type="submit" value="上傳"/> </form> |
public class UploadAction extends BaseAction {
private File file ;//指jsp的name屬性,要上傳的檔案 xxx
private String fileContentType;//檔案的型別 xxxContentType
private String fileFileName;//檔名 xxxFileName
private String serverDir = "/upload";//建立一個檔案存放圖片
public String upload() {//上傳
System.out.println(fileContentType);
System.out.println(fileFileName);
String realPath = getRealPath(serverDir +"/" +fileFileName);
System.out.println(realPath);
//引數1:本地檔案 引數2:在伺服器上生成的檔案
try {
FileUtils.copyFile(file, new File(realPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
//真實路徑指Linux下的上傳檔案的具體所在位置
private String getRealPath(String path) {
return application.getRealPath(path);
}
public String openAs() {//展示圖片
//這是我定死的資料
String type="image/gif";
String name = "2.gif";
response.setContentType(type);//設定開啟圖片是什麼格式的
//響應頭
response.setHeader("Content-Disposition","filename=" + name);//檔名
/**
* 將遠端的圖片輸出到本地
* 資料來源inputStream :遠端 new File(realPath)
* 目的:輸出到本地的jsp response.getoutStream
*/
String realPath = getRealPath(serverDir +"/" +name);
try {
FileUtils.copyFile(new File(realPath),response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String download() {//下載
String type="image/gif";
String name = "2.gif";
response.setContentType(type);//設定開啟圖片是什麼格式的
//響應頭
response.setHeader("Content-Disposition","attachment;filename=" + name);//檔名
/**
* 將遠端的圖片輸出到本地
* 資料來源inputStream :遠端 new File(realPath)
* 目的:輸出到本地的jsp response.getoutStream
*/
String realPath = getRealPath(serverDir +"/" +name);
try {
FileUtils.copyFile(new File(realPath),response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
}
注意的點:
1.name屬姓的file要在Action找中的file一致,不然會報
Struts Problem Report
java.lang.NullPointerException: Source must not be null ----這樣的錯誤
2.enctype="multipart/form-data" 這是在form表單裡的,是將檔案以二進位制的形式上傳,這樣可以實現多種型別的檔案上傳。
3.attachment是附件;押扣的意思。在展示時是不用在響應頭裡面寫的,而要在下載圖片的時候加上,不然當點選下載時會指出現圖片,而不會下載。
4、記得要將action方法中的的三個屬性寫上set/get方法,不然會獲取不到值。
最後總結一下下:
今天學習了Struts的檔案上傳下載,剛開始真的弄不懂,後來慢慢理解了發現還是挺好玩的。喜歡自己認真思考的樣子嘻嘻嘻嘻嘻。。。。Struts其實也挺好玩,標籤很特別。
金無赤金,人無完人”。fighting