Struts2(5) 攔截器和檔案上傳
阿新 • • 發佈:2018-12-16
Interceptor
與filter的區別:先過filter再過interceptor
怎麼設定攔截器: 在配置檔案struts-sy.xml的 package裡面配置
<!-- 配置2個攔截器 class指的是攔截器的全類名 --> <interceptors> <interceptor name="oneInterceptor" class="com.zking.five.interceptor.OneInterceptor"></interceptor> <interceptor name="twoInterceptor" class="com.zking.five.interceptor.TwoInterceptor"></interceptor> </interceptors> <action name="interceptorAction" class="com.zking.five.web.InterceptorAction" > <interceptor-ref name="oneInterceptor"></interceptor-ref> <interceptor-ref name="twoInterceptor"></interceptor-ref> </action> <action name="interceptorAction" class="com.zking.five.web.InterceptorAction" > <interceptor-ref name="oneInterceptor"></interceptor-ref> <interceptor-ref name="twoInterceptor"></interceptor-ref> </action>
檔案上傳:
檔案上傳的三種方案:
- 將上傳的檔案以二進位制形式存放到資料庫 OA系統 activity工作流程
- 將檔案上傳到檔案伺服器(硬碟足夠大)中
- 將檔案上傳到tomcat所在的web伺服器中
真實路徑與虛擬路徑的概念:
- 所謂的真實路徑指的是在自己的電腦上能夠看到的路徑
- 所謂的虛擬路徑,在自己的電腦上看不到的,路徑在別人的電腦上能夠看到
UploadAction :
package com.zking.five.web; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.zking.four.web.BeanAction; public class UploadAction extends BeanAction { private File file;//變數名指的是jsp的name屬性,就是你要上傳的檔案 private String fileContentType;//xxxContentType private String fileFileName;//xxxFileName private String serverDiv = "/upload";//資料夾 //上傳 public String upload() { System.out.println(fileContentType); System.out.println(fileFileName); String realPath = getRealPath(serverDiv + "/"+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下的檔案的所在位置 * @param path * @return */ private String getRealPath(String path) { // TODO Auto-generated method stub return application.getRealPath(path); } //檢視圖片 public String openAs() { String type = "image/jpeg"; String name = "u=2988065565,1735810570&fm=27&gp=0.jpg"; response.setContentType(type);//內容型別 response.setHeader("Content-Disposition","filename=" + name);//檔名 /* * 將遠端的圖片輸出到本地 * 資料來源inputstream:遠端 new file(realPath) * 目的:輸出到本地jsp response.getOutputStream() */ String realPath = getRealPath(serverDiv + "/"+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/jpeg"; String name = "u=2988065565,1735810570&fm=27&gp=0.jpg"; response.setContentType(type); response.setHeader("Content-Disposition","attachment;filename=" + name);//檔名 /* * 將遠端的圖片輸出到本地 * 資料來源inputstream:遠端 new file(realPath) * 目的:輸出到本地jsp response.getOutputStream() */ String realPath = getRealPath(serverDiv + "/"+name); try { // FileUtils.copyFile(new File(realPath), response.getOutputStream()); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); copyStream(in, out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private void copyStream(BufferedInputStream in,BufferedOutputStream out) throws IOException { byte[] bbuf = new byte[1024]; int len = 0; while((len = in.read(bbuf)) !=-1 ) { out.write(bbuf,0,len); } in.close(); out.close(); } 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; } }
配置:
<action name="uploadAction_*" class="com.zking.five.web.UploadAction" method="{1}" >
<result name="success" >/success.jsp</result>
</action>
jsp頁面demp1 :
<h1>Struts上傳下載</h1> <s:form namespace="/sy" action="uploadAction_upload" enctype="multipart/form-data" method="post" > <input type="file" name="file" > <input type="submit" value="上傳" > </s:form>
jsp頁面success :
<h1>直接開啟圖片</h1>
<s:url var="openAsurl" namespace="/sy" action="uploadAction_openAs.action" ></s:url>
<s:property value="openAsurl" />
<img alt="" src='<s:property value="openAsurl" />'>
<h1>過載圖片</h1>
<s:url var="downloadAsurl" namespace="/sy" action="uploadAction_download.action" ></s:url>
<s:a href="%{#downloadAsurl}" >下載</s:a>