日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

拦截器与文件上传

發布時間:2025/7/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 拦截器与文件上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

攔截器與文件上傳


三種上傳方案:
1、上傳到tomcat服務器(不能及時刷新)
2、上傳到指定文件目錄,添加服務器與真實目錄的映射關系,從而解耦上傳文件與tomcat的關系
文件服務器(目前公司用的最常用的方法)
3、在數據庫表中建立二進制字段,將圖片存儲到數據庫(處理上百萬的數據不方便)

?

完成圖片上傳:

寫兩個方法:1.上傳圖片2.跳轉文件上傳頁面。

1 private File file; 2 private String fileContentType; 3 private String fileFileName; 4 /** 5 * 直接上傳圖片 6 * @return 7 * @throws IOException 8 */ 9 public String upload() throws IOException { 10 //注意:在Linux下是沒有E盤的 linnx下只有一個盤符,那么意味著,當打包到linux服務器的時候需要改動代碼 11 //這個時候通常是這么解決的,將targetPath對應目錄串,配置到資源文件中,通過Properties類進行動態讀取 12 //那么當需要將項目發布到Linux服務器的時候,只需要改變xxx.preperties文件中targetPath=/caoluo/img 13 14 //實際圖片存儲的位置 15 String targetDir="D:/雜七雜八/上傳圖片"; 16 17 //存到數據庫中的地址 18 String severPath="/upload"; 19 FileUtils.copyFile(file, new File(targetDir+"/"+fileFileName)); 20 //注意: 數據庫存放是網絡請求地址,而不是本地存放圖片地址 21 clz.setPic(severPath+"/"+fileFileName); 22 this.clzDao.edit(clz); 23 return "toList"; 24 } 25 /** 26 * 跳轉文件上傳頁面 27 * @return 28 */ 29 public String preUpload() { 30 Clazz c=this.clzDao.list(clz, null).get(0); 31 request.setAttribute("clz", c); 32 return "toUpload"; 33 }

struts_sy.xml配置

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 <package name="sy" extends="base" namespace="/sy"> 7 <action name="/clz_*" class="com.web.ClazzAction" method="{1}"> 8 <result name="list">/clzList.jsp</result> 9 <result name="preSave">/clzEdit.jsp</result> 10 <result name="toList" type="redirectAction">/clz_list</result> 11 <result name="toUpload">/upload.jsp</result> 12 </action> 13 </package> 14 </struts>

然后寫一個上傳圖片的jsp頁面

1 <body> 2 <form action="${pageContext.request.contextPath }/sy/clz_upload.action" method="post" enctype="multipart/form-data"> 3 <input type="hidden" name="cid" value="${clz.cid }"> <br> 4 <input type="hidden" name="cname" value="${clz.cname }"> <br> 5 <input type="hidden" name="cteacher" value="${clz.cteacher }"> <br> 6 <!-- 注意:name對應的值決定了 自控制器action屬性的命名 --> 7 <input type="file" name="file"> 8 <input type="submit"> 9 </form> 10 </body>

在server.xml中添加一行代碼

<!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/> <Context path="/T_caoluo/upload" docBase="D:/雜七雜八/上傳圖片/"/><Context docBase="T_caoluo" path="/T_caoluo" reloadable="true" source="org.eclipse.jst.jee.server:T_caoluo"/></Host></Engine></Service>

首頁顯示圖片:

1 <c:forEach items="${clzList }" var="c"> 2 <tr> 3 <td>${c.cid }</td> 4 <td>${c.cname }</td> 5 <td>${c.cteacher }</td> 6 <td> 7 <img alt="" style="width: 60px;height: 60px" src="${pageContext.request.contextPath }${c.pic }"> 8 </td> 9 <td> 10 <a href="${pageContext.request.contextPath }/sy/clz_preSave.action?cid=${c.cid }">修改</a>&nbsp;&nbsp; 11 <a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${c.cid }">刪除</a>&nbsp;&nbsp; 12 <a href="${pageContext.request.contextPath }/sy/clz_preUpload.action?cid=${c.cid }">上傳圖片</a>&nbsp;&nbsp; 13 </td> 14 </tr> 15 </c:forEach>

?

然后運行:

選擇一個圖片確認上傳

?

效果圖:

攔截器:

1 public class OneInterceptor implements Interceptor{ 2 3 @Override 4 public void destroy() { 5 // TODO Auto-generated method stub 6 7 } 8 9 @Override 10 public void init() { 11 // TODO Auto-generated method stub 12 13 } 14 15 @Override 16 public String intercept(ActionInvocation arg0) throws Exception { 17 System.out.println("======OneInterceptor======1"); 18 String invoke=arg0.invoke(); 19 System.out.println("=======OneInterceptor======2"); 20 return invoke; 21 } 1 public class TwoInterceptor implements Interceptor{ 2 3 @Override 4 public void destroy() { 5 // TODO Auto-generated method stub 6 7 } 8 9 @Override 10 public void init() { 11 // TODO Auto-generated method stub 12 13 } 14 15 @Override 16 public String intercept(ActionInvocation arg0) throws Exception { 17 System.out.println("======TwoInterceptor======1"); 18 String invoke=arg0.invoke(); 19 System.out.println("=======TwoInterceptor======2"); 20 return invoke; 21 } 22 23 }

配置:

<package name="sy" extends="base" namespace="/sy">
<interceptors>
<interceptor name="one" class="com.interceptor.OneInterceptor"></interceptor>
<interceptor name="two" class="com.interceptor.TwoInterceptor"></interceptor>
</interceptors>

<action name="/clz_*" class="com.web.ClazzAction" method="{1}">
<interceptor-ref name="one"></interceptor-ref>
<interceptor-ref name="two"></interceptor-ref>
<result name="list">/clzList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="redirectAction">/clz_list</result>
<result name="toUpload">/upload.jsp</result>
</action>
</package>

?

攔截成功;

轉載于:https://www.cnblogs.com/AluoKa/p/11169139.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的拦截器与文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。