zoukankan      html  css  js  c++  java
  • 多文件上传与拦截器

    @Controller
    public 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类
    @Controller
    public 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/>
  • 相关阅读:
    Cartographer源码阅读(1):程序入口
    ROS开发与常用命令
    实时Cartographer测试(1)
    Cartographer安装
    ROS安装(2)
    Linux学习和ROS安装(1)
    无法启动程序
    c# 获取端口的连接数,网站的连接数
    SignarL服务器端发送消息给客户端的几种情况
    c#操作IIS之IISHelper
  • 原文地址:https://www.cnblogs.com/wangbenqing/p/7472663.html
Copyright © 2011-2022 走看看