zoukankan      html  css  js  c++  java
  • springmvc 文件上传和拦截器

    文件上传:

    单文件:

    @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>
    

      

            

  • 相关阅读:
    TensorFlow实现线性回归
    在window10平台下安装TensorFlow(only cpu)
    毕设——基于深度学习的恶意代码检测
    play ball小游戏-Alpha版本发布说明
    支持3000万道四则运算题目生成的小学生毁灭姬
    图片集
    个人项目WordCount(C++/QT)
    自我介绍+软工5问
    大数据应用技术课程实践--选题与实践方案
    15 手写数字识别-小数据集
  • 原文地址:https://www.cnblogs.com/xu06123/p/8694317.html
Copyright © 2011-2022 走看看