• SpringMVC学习四(文件上传/拦截器)


    1.文件上传

    1.1预备工作,需要两个jar包(Fileupload

     jar包下载路径:

    [点击下载https://github.com/suyirulan/putao/tree/master/fileupload_jar]

    1.2上传文件的jsp页面如下,注意上传文件时表单form标签的属性method="post" enctype="multipart/form-data"是必须的,并且文件的name=" "也是必须的

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 <meta charset="UTF-8">
     7 <title>上传文件</title>
     8 </head>
     9 <body>
    10     <form action="addStudent" method="post" enctype="multipart/form-data">
    11         姓名:<input type="text" name="name"/><br>
    12         年龄:<input type="text" name="age"/><br>
    13         头像:<input type="file" name="headphoto"/><br>
    14         <input type="submit" value="上传"/>
    15     </form>
    16 </body>
    17 </html>

    1.3在springmvc配置文件中上传解析器

     其中value="99999999"为限制上传文件的最大字节(字节计算:1M=1024k=1024*1024=1048576字节

    1 <!-- 文件上传解析器 -->
    2 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    3     <property name="maxUploadSize" value="99999999"></property>
    4 </bean>

    1.4上传文件的Controller代码

     1 @Controller
     2 public class StudentController {
     3     
     4     @RequestMapping("addStudent")
     5     public String addStudent(Student stu,HttpServletRequest request,MultipartFile headphoto,Model model) {
     6         //1.获取上传文件真实保存路径
     7         String path = request.getServletContext().getRealPath("/headphoto");
     8         //2.创建一个文件对象
     9         File file=new File(path);
    10         //若没有此文件则创建
    11         if(!file.exists()) {
    12             file.mkdirs();
    13         }
    14         //3.获取文件名
    15         String filename=System.currentTimeMillis()+headphoto.getOriginalFilename();
    16         
    17         stu.setPhoto("headphoto/"+filename);
    18         
    19         File targetFile =new File(path+"/"+filename);
    20         
    21         try {
    22             FileUtils.writeByteArrayToFile(targetFile, headphoto.getBytes());
    23         } catch (IOException e) {
    24             // TODO Auto-generated catch block
    25             e.printStackTrace();
    26         }
    27         
    28         model.addAttribute("stu",stu);
    29         
    30         return "index";
    31     }
    32 }

    进行测试

    上传成功

     2.拦截器(拦截的都是控制层地址,filter:页面地址请求都可以拦截)

    2.1创建一个类,实现接口Handlerinterceptor

    2.2重写接口中的方法

     1 public class MyInterceptor implements HandlerInterceptor{
     2 
     3     @Override
     4     public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
     5             throws Exception {
     6         // TODO Auto-generated method stub
     7         
     8     }
     9 
    10     @Override
    11     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
    12             throws Exception {
    13         // TODO Auto-generated method stub
    14         
    15     }
    16 
    17     @Override
    18     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
    19         Object name = request.getSession().getAttribute("name");
    20         if(name!=null) {
    21             return true;
    22         }else {
    23             System.out.println("拦截器");
    24             return false;
    25         }
    26     }
    27
    28 }

    2.3把创建的类配置到SpringMVC文件中,其中<mvc:mapping path=" "/>为拦截的,<mvc:exclude-mapping path=" "/>为放行的

    1 <!-- 拦截器 -->
    2     <mvc:interceptors>
    3         <mvc:interceptor>
    4             <mvc:mapping path="/user/**"/>
    5             <mvc:exclude-mapping path="/user/login1"/>
    6             <mvc:exclude-mapping path="/user/denglu"/>
    7             <bean class="com.zhiyou100.zhl.interceptor.MyInterceptor"></bean>
    8         </mvc:interceptor>
    9     </mvc:interceptors>

    Fileupload

  • 相关阅读:
    安装原版Win8.1并激活
    Java8 List<对象> 转 Set、Map(高级)、排序、分组、统计
    SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)
    SpringCloud第一弹(入门)
    SpringBoot+Shiro+Redis共享Session入门小栗子
    Go语言(IDEA下+Eclipse下)Hello World
    Linux学习杂谈
    小孩儿才分对错,成年人只看利弊
    Xshell5
    91 Testing MySQL学习总结
  • 原文地址:https://www.cnblogs.com/murmansk/p/11458368.html
Copyright © 2020-2023  润新知