文件上传:

单文件:

@Controller
public class FirstController {@RequestMapping("/first")public String doFrist(MultipartFile upload, HttpSession session){//文件不能为空if (upload.getSize()>0){String childpath=upload.getOriginalFilename();//限定类型/*  if (childpath.endsWith("jsp")){}*/String fatherpath=session.getServletContext().getRealPath("/upload");System.out.print(fatherpath);//拼接路径File file=new File(fatherpath,childpath);try {//上传upload.transferTo(file);} catch (IOException e) {e.printStackTrace();}}else{System.out.print("文件为空");}return "/index.jsp";}
}

  配置文件:

<context:component-scan base-package="day013shangchuan"></context:component-scan><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="50000000"></property> //上传文件的总大小<property name="maxUploadSizePerFile" value="50000"></property>//单个文件的大小<property name="defaultEncoding" value="utf-8"></property> //编码的字符集</bean><mvc:annotation-driven></mvc:annotation-driven>

  jsp页面::

<form action="/first" method="post" enctype="multipart/form-data">文件:<input type="file" name="upload"><input type="submit">
</form>

  多文件上传:

@Controller
public class FirstControllers {@RequestMapping("/firsts")public String doFrist(@RequestParam MultipartFile [] upload, HttpSession session){for (MultipartFile item:upload){//文件不能为空if (item.getSize()>0){String childpath=item.getOriginalFilename();//限定类型/*  if (childpath.endsWith("jsp")){}*/String fatherpath=session.getServletContext().getRealPath("/upload");System.out.print(fatherpath);//拼接路径File file=new File(fatherpath,childpath);try {//上传item.transferTo(file);} catch (IOException e) {e.printStackTrace();}}else{System.out.print("文件为空");}}return "/index.jsp";}
}

 配置和上面的一样

jsp页面:

<form action="/first" method="post" enctype="multipart/form-data">文件:<input type="file" name="upload">文件1:<input type="file" name="upload">文件2:<input type="file" name="upload"><input type="submit">
</form>

拦截器:

public class FirstInter implements HandlerInterceptor {public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {System.out.println("111");return true;}public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println("333");}public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println("444");}}

  

配置文件:

<context:component-scan base-package="day013shangchuan"></context:component-scan><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="50000000"></property><property name="maxUploadSizePerFile" value="50000"></property><property name="defaultEncoding" value="utf-8"></property></bean><mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><bean class="day014lanjie.FirstInter"></bean></mvc:interceptor></mvc:interceptors><mvc:annotation-driven></mvc:annotation-driven>

  

转载于:https://www.cnblogs.com/xu06123/p/8694317.html

springmvc 文件上传和拦截器相关推荐

  1. SpringMVC处理Json、文件上传、拦截器

    SpringMVC处理Json.文件上传.拦截器 : 处理JSON 链接 http://repo1.maven.org/maven2/com/fasterxml/jackson/core/ 步骤 编写 ...

  2. 【SSM框架系列】SpringMVC的文件上传、拦截器及异常处理

    SpringMVC的文件上传 服务器端实现文件上传的技术有很多种,Servlet3.0,FileUtils,框架等等,都可以实现文件上传,不管使用哪一种上传技术,都必须满足三要素: 表单项type=& ...

  3. SpringMVC的文件上传和拦截器

    目录 一.文件上传 1.概述 2.案例1 3.案例2 二.拦截器 1.HandlerInterceptor接口 2.案例:拦截器实现用户权限验证 一.文件上传 1.概述 SpringMVC会将上传文件 ...

  4. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  5. SpringBoot→请求参数、返回参数、文件上传、拦截器、异常处理、定时任务、异步任务

    web请求static静态资源 localhost:8080/cat.png localhost:8080/test.html localhost:8080/abc/test.html localho ...

  6. springmvc请路径参数,springmvc-json-ajax使用,springmvc文件上传,springmvc文件下载,springmvc拦截器(内容较多)

    按照操作,绝对都可以实现,亲测.....(项目所有文件和目录结构全都放上去了) idea工具,创建maven项目 第一步: 放置各种配置文件: pom.xml <?xml version=&qu ...

  7. SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器

    SpringMVC入门(二)-- 参数的传递.Controller方法返回值.json数据交互.异常处理.图片上传.拦截器 参考文章: (1)SpringMVC入门(二)-- 参数的传递.Contro ...

  8. SpringMVC文件上传功能MultipartFile参数为空

    最近简单实现了一个springmvc文件上传功能,点了上传按钮一直报空指针,问题解决了mark一下留作以后查阅,如有搜到这篇文章的仅供参考,先上主要代码.springmvc.xml配置如下: < ...

  9. SpringMVC——文件上传下载,异步请求和SSM整合

    一,SpringMVC文件上传下载 1.1 同步 1.1.1 文件上传 第一步:定义上传表单 <form action="${pageContext.request.contextPa ...

最新文章

  1. 青龙羊毛——去趟转(搬运,非原创)
  2. 如何将Visio图形转换成EPS格式【mark from百度知道】
  3. java 进制转换 十进制转二,八,十六进制
  4. Dubbo(三) 消费者、提供者工程搭建并实现远程调用
  5. python入门——P45魔法方法:属性访问
  6. linux 下解决arp病毒攻击时上网问题的最简单的治标办法
  7. ps无法加载扩展未经正确签署解决方法
  8. PSO最佳适应度收敛曲线
  9. 论文写作中插入公式间距变大怎么办?
  10. 赛迪中国ECM市场报告解读一:市场需求在2023年将达到275亿元
  11. mysql数据库now_MySQL NOW() 函数
  12. 清理C盘空间,无需命令行,可清理几十G内存,实测有效
  13. 史上最全的疫情历史数据
  14. 商用台式计算机施工方案,HP Elite 8300 商用台式电脑 - 主板说明
  15. 我为什么要写微信公众号
  16. 【附源码】Python计算机毕业设计手游账号交易系统
  17. ALSA 配置文件简介
  18. 【几乎最全/全网最长的 2 万 字】前端工程化完整流程:从头搭到尾(vue3 + vite + qiankun + docker + tailwindcss + iview......)
  19. 如何使用万能地图下载器下载矢量建筑边界
  20. 从码力到算力的“狂飙”,探元宇宙的未来风向

热门文章

  1. 使用MySQL Workbench远程管理工具进行远程管理Mysql的错误及解决办法
  2. 北大OJ百练——4074:积水量(C语言)
  3. C++书籍笔记目录汇总【目前1本读书笔记(持续更新中……)】
  4. [转]oracle查看数据文件, 控制文件, 及日志文件命令
  5. mysql使用数据库_MySQL数据库的常用操作
  6. python 计量_距离度量以及python实现(二)
  7. mysql的付费功能_MYSQL对游戏用户付费行为分析
  8. 这个网站绝了,收录近600条Linux系统命令
  9. 让容器跑得更快:CPU Burst 技术实践
  10. 透过 3.0 Preview 看 Dubbo 的云原生变革