標籤:class 檔案 ons origin rac 路徑 多檔案上傳 nsf enc
@Controllerpublic class MostFileController { //多檔案上傳@RequestMapping("/first") public String doFlrat(@RequestParam MultipartFile[] upload, HttpSession session) { System.out.println("*******************"); for (MultipartFile item:upload) { if(item.getSize()>0){ //使用者是否選擇了檔案 //擷取到使用者上傳的檔案名稱String chilPath=item.getOriginalFilename(); //檔案段名稱if(chilPath.endsWith(".jpg")||chilPath.endsWith("gif")||chilPath.endsWith("png")){ //將行對路徑轉換成絕對路徑String paraPath=session.getServletContext().getRealPath("/uplode"); //將file寫入指定的路徑File filePath=new File(paraPath,chilPath); try { //將檔案記憶體運輸到指定的檔案中item.transferTo(filePath); } catch (IOException e) { e.printStackTrace(); return "/Fileuplode.jsp"; } }else { return "/Fileuplode.jsp"; } }else { return "/Fileuplode.jsp"; } } return "/index.jsp"; }
@RequestMapping("/first2")public String doFirst2(MultipartFile upload,HttpSession session){ System.out.println("****************************88"); if(upload.getSize()>0){ //使用者是否選擇了檔案 //擷取到使用者上傳的檔案名稱String chilPath=upload.getOriginalFilename(); //檔案簡短名稱if(chilPath.endsWith(".jpg")||chilPath.endsWith("gif")||chilPath.endsWith("png")){ //將相對路徑轉化成絕對路徑String paratPath=session.getServletContext().getRealPath("/uplode"); //將file寫入指定的路徑File filePath=new File(paratPath,chilPath); try { //將檔案記憶體運輸到指定的檔案中upload.transferTo(filePath); } catch (IOException e) { e.printStackTrace(); return "/index.jsp"; } }else { return "/Fileuplode.jsp"; } }else { return "/Fileuplode.jsp"; } return "/Fileuplode.jsp"; }
}
MostFileupdlo.xml配置:
<!--配置包掃描器--><context:component-scan base-package="cn.mostFileupload"></context:component-scan> <!--設定檔上傳的專用類--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="5000000"></property></bean><mvc:annotation-driven/>
Fileuplode.jsp頁面:
</head><body><h1>檔案上傳</h1><form action="/first" method="post" enctype="multipart/form-data"> 檔案1 <input type="file" name="upload"/> 檔案2 <input type="file" name="upload"/> 檔案3 <input type="file" name="upload"/> <input type="submit"/></form></body>
Struts2攔截器: exception 異常攔截器
params 參數攔截器
il8n 國際化攔截器
fileupload 檔案上傳攔截器
validation 校正攔截器
Struts2中處理的請求的組件是:Action
SpringMVC中處理請求的組件是:Controller
JSP中處理請求的組件是: servlet
攔截器HandlerInterceptor的三種方法:(1)perHandle() (2)postHandle() (3)afterCompletion()
註冊攔截器: **匹配0或者更多的目錄
*匹配0或者任意的字串
攔截器:
建立Myhanderinter類並整合HandlerInterceptor介面中的方法:
public class Myhanderinter implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("perHandle+========================================="); return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("posthandle-------------------------------------"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterHandle===================================="); }}
//在建立一個intercfeption類
@Controllerpublic class intercfeption { @RequestMapping("/first") public String doInter(){ System.out.println("Handle====================================="); return "index.jsp"; }}
HandleInter.xml配置:
<!--配置包掃描器--><context:component-scan base-package="cn.Handerinter"></context:component-scan> <!--註冊攔截器--><mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="cn.Handerinter.Myhanderinter"></bean> </mvc:interceptor></mvc:interceptors><mvc:annotation-driven/>
多檔案上傳與攔截器