struts2攔截器和檔案上傳下載
阿新 • • 發佈:2018-11-10
一、攔截器(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
<interceptorname="oneInter"class=“com.zking.struts.four.OneInterceptor”>
<interceptorname="twonter"class=“com.zking.struts.four.TwoInterceptor”>
strut檔案上傳
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 Reportjava.lang.NullPointerException: Source must not be null ----這樣的錯誤2.enctype=“multipart/form-data” 這是在form表單裡的,是將檔案以二進位制的形式上傳,這樣可以實現多種型別的檔案上傳。3.attachment是附件;押扣的意思。在展示時是不用在響應頭裡面寫的,而要在下載圖片的時候加上,不然當點選下載時會指出現圖片,而不會下載。4、記得要將action方法中的的三個屬性寫上set/get方法,不然會獲取不到值。