文件上传与拦截器
来源:互联网 发布:pm2.5数据接口 编辑:程序博客网 时间:2024/06/13 20:17
单文件上传:
@Controllerpublic class FileController { @RequestMapping("/first") public String doFirst(MultipartFile upload, HttpSession session){
System.out.println("*******************"); if (upload.getSize() > 0) { //用户是否选择了文件 //获取到用户上的文件名称 String chilpath = upload.getOriginalFilename(); //文件短路径 //将相对路径转换成绝对路径 String parePath = session.getServletContext().getRealPath("/uplode"); //将file写入指定的路径 File filePath = new File(parePath, chilpath); try { upload.transferTo(filePath); return "/Fileuplode.jsp"; } catch (IOException e) { e.printStackTrace(); return "/list.jsp"; } }else { return "/list.jsp"; } }}
Fileupdlo.xml配置:
多文件上传:<!--配置包扫描器--><context:component-scan base-package="cn.Fileuplode"></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/>
@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/>
阅读全文
0 0
- 文件上传与拦截器
- 双文件上传与拦截器
- 19_Struts 2.x 拦截器与文件上传
- 文件上传action与拦截器的实现
- 【Struts2】文件上传系统、拦截器与国际化
- struts2 拦截器上传文件
- Struts2文件上传(二)-文件上传拦截器
- Struts2上传文件拦截器实现文件上传
- Struts2 文件上传拦截器配置问题
- struts2 文件上传的拦截器问题
- Struts2文件上传 +自定义拦截器
- Struts2 文件上传fileUpload拦截器不起作用
- struts2--文件上传和uploadFile拦截器
- struts2--文件上传和uploadFile拦截器
- struts2--文件上传和uploadFile拦截器
- struts2--文件上传和uploadFile拦截器
- spring+DelegatingFilterProxy拦截器实现文件上传
- struts2--文件上传和uploadFile拦截器
- 阿里数据库连接池之Druid 的介绍及配置
- 大话PCIe:Linux访问PCIe空间
- codeforces 831D
- Android Servicer 播放音乐
- 你所知道的DOM事件
- 文件上传与拦截器
- UVA
- 记录下使用Ambari部署HDP集群的过程
- 洛谷 P2827 蚯蚓
- 嵌入式面试全攻略,记我的一次校招
- POJ2185 最小覆盖矩阵 (二维KMP)
- BZOJ 3295 [Cqoi2011]动态逆序对
- 51 nod 算法马拉松28 先序遍历与后序遍历
- C++类与对象学习