分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

多文件上传与拦截器

发布时间:2023-09-06 01:08责任编辑:赖小花关键词:文件上传
@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/>

多文件上传与拦截器

原文地址:http://www.cnblogs.com/wangbenqing/p/7472663.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved