栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring文件上传拦截器及异常处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring文件上传拦截器及异常处理

1文件上传

当enctype="multipart/form-data"时request.getParameter()方法失效

单文件上传
导入坐标

   
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-io
            commons-io
            2.6
        

名称
文件
文件
文件

配置文件上传解析器
在spring-mvc.xml中加入

   
       
        
       
        
        
        
    

这里上传到webapp下

@RequestMapping(value = "/quick22")
    @ResponseBody //不进行页面跳转不进行数据回写响应体为空
    public void save22(String username, MultipartFile uploadFile1, MultipartFile uploadFile2) throws IOException {
        System.out.println(username);
        //
        String originalFilename1 = uploadFile1.getOriginalFilename();
        String originalFilename2 = uploadFile2.getOriginalFilename();
        //获取servletContext对象
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        ServletContext servletContext = 
//获取真实路径
webApplicationContext.getServletContext();
        String realPath = servletContext.getRealPath("/");
        //上传到哪儿
        uploadFile1.transferTo(new File(realPath+originalFilename1));
        uploadFile2.transferTo(new File(realPath+originalFilename2));
    }

多文件上传
前台

名称
文件
文件
文件

客户端用数组接循环存储就行

    @RequestMapping(value = "/quick23")
    @ResponseBody //不进行页面跳转不进行数据回写响应体为空
    public void save23(String username, MultipartFile[] uploadFile) throws IOException {
    
        System.out.println(username);
        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
        ServletContext servletContext = webApplicationContext.getServletContext();
        String realPath = servletContext.getRealPath("/");
        
        for (MultipartFile multipartFile : uploadFile) {
            String originalFilename = multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File(realPath+originalFilename
            ));
        }
    }

2,springMVC拦截器
类似Servlet开发中的过滤器Filter 用于处理器的预处理和后处理
不会拦截js,css html jsp
1)创建MyInterceptorl一个类实现implements HandlerInterceptor
有三个方法

//在目标方法值前
  @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 return flase;//放不放行
 }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      //再返回视图时执行
     }

//在返回视图后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}   
    
    
        
            
            
            modelAndView.addObject("info","类转换异常");
        }
        
        modelAndView.setViewName("error");

        return modelAndView;
    }
}

在前端取数据${info}

   
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/851388.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号