多文件上傳與攔截器
阿新 • • 發佈:2017-09-04
quest 配置文件 mon index.jsp except for can ring servle
@Controller public 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=newFile(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類
@Controller public 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/>
多文件上傳與攔截器