【springMVC】高级部分

拦截器

1 拦截器( Interceptor)

  • 是一种动态拦截方法调用的机制
# 作用:
	1. 在指定的方法调用前后执行预先设定后的的代码
	2. 阻止原始方法的执行

# 核心原理: 
	AOP思想
# 拦截器链:
	多个拦截器按照一定的顺序,对原始被调用功能进行增强  

2 拦截器使用步骤

1 实现HandlerInterceptor接口

/**
 *  三个方法的运行顺序为    preHandle -> postHandle -> afterCompletion
 *  如果preHandle返回值为false,三个方法仅运行preHandle
 */
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("前置运行----a1");
        //返回值为false将拦截原始处理器的运行
        //如果配置多拦截器,返回值为false将终止当前拦截器后面配置的拦截器的运行
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("后置运行----b1");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("完成运行----c1");
    }
}

2 配置拦截器

  <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/m8"/>
            <bean class="com.xinzhi.intercepter.MyInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

3 拦截器配置项

<mvc:interceptors>
    <!--开启具体的拦截器的使用,可以配置多个-->
    <mvc:interceptor>
        <!--设置拦截器的拦截路径,支持*通配-->
        <!--/**         表示拦截所有映射-->
        <!--/*          表示拦截所有/开头的映射-->
        <!--/user/*     表示拦截所有/user/开头的映射-->
        <!--/user/add*  表示拦截所有/user/开头,且具体映射名称以add开头的映射-->
        <!--/user/*All  表示拦截所有/user/开头,且具体映射名称以All结尾的映射-->
        <mvc:mapping path="/*"/>
        <mvc:mapping path="/**"/>
        <mvc:mapping path="/handleRun*"/>
        <!--设置拦截排除的路径,配置/**或/*,达到快速配置的目的-->
        <mvc:exclude-mapping path="/b*"/>
        <!--指定具体的拦截器类-->
        <bean class="MyInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

4 拦截器的使用场景

1)日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。

2)权限检查:如登录检测,进入处理器检测是否登录,如果没有直接返回到登录页面;

3)性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反向代理,如apache可以自动记录);

5 拦截器登录案例

@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String uri = request.getRequestURI();
        if(uri.equal("/login")){
            return true;
        }
        HttpSession session = request.getSession();
        Object user = session.getAttribute("USER_SESSION");
        if(user!=null){
            return true;
        }
        request.setAttribute("msg","未登陆状态");
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
    }

<mvc:interceptors>
    	<mvc:interceptor>
        	<mvc:mapping path="/**" />
        	<bean class="com.soft.interceptor.LoginInterceptor" />
    	</mvc:interceptor>
</mvc:interceptors>	

7 文件上传

1 导入maven依赖

commons-fileupload commons-fileupload 1.4

2 前端页面

<form action="/fileupload" method="post" enctype="multipart/form-data">
    上传LOGO: <input type="file" name="file"/><br/>
    <input type="submit" value="上传"/>
</form>

3 配置多媒体解析器

<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
package com.xinzhi.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;

@Controller
public class FileUploadController {

    /**
     * 上传页面跳转
     * @return
     */
    @RequestMapping(value = "/upload",method = RequestMethod.GET)
    public String upload(){
        return "fileUpload";
    }

    /**
     * 下载页面跳转
     * @return
     */
    @RequestMapping(value = "/down",method = RequestMethod.GET)
    public String down(){
        return "download";
    }

    /**
     * 上传逻辑
     * @param uploadfile 上传的文件数组
     * @return
     */
    @RequestMapping(value = "/fileUpload",method = RequestMethod.POST)
    public String uploadFile(MultipartFile[] uploadfile){
        for (MultipartFile file : uploadfile) {
            //获取文件名称
            String filename = file.getOriginalFilename();
            //存在服务器上名称的修改
            filename = UUID.randomUUID()+"_"+filename;
            // 定义服务器上的存储路径
            String dirPath = "F:/file/";
            File filePath = new File(dirPath);
            // 判断路径是否存在,不存在就创建
            if(!filePath.exists()){
                filePath.mkdir();
            }
            try {
                // 文件上传的核心
                file.transferTo(new File(dirPath+filename));
            } catch (IOException e) {
                e.printStackTrace();
                return "error";
            }
        }
        return "success";
    }


    @RequestMapping("/download")
    public ResponseEntity<byte[]> fileDownload(HttpServletRequest request,String filename) throws UnsupportedEncodingException {
        //指定文件下载地址的目录   filename -> 美女.jpg
        String dirPath = "D:/file";
        // 指定下载的文件名称
        File file = new File(dirPath + File.separator + filename);
        HttpHeaders headers = new HttpHeaders();
        // 解决不同浏览器之间乱码问题
        filename = getFilename(request, filename);
        //告诉浏览器,打开方式(附件)
        headers.setContentDispositionFormData("attachment",filename);
        //以二进制字节流的方式下载
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            return new ResponseEntity<>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>(e.getMessage().getBytes(), HttpStatus.EXPECTATION_FAILED);
        }
    }

    /**
     * 不同浏览器的版本兼容
     * @param request
     * @param filename
     * @return
     * @throws UnsupportedEncodingException
     */
    private String getFilename(HttpServletRequest request,String filename) throws UnsupportedEncodingException {
        //判断是不是IE内核的关键字
        String[] IEBrowerKeyWords = {"MSIE","Trident","Edge"};
        String keywords = request.getHeader("User-Agent");
        for (String keyWord : IEBrowerKeyWords) {
            if(keywords.contains(keyWord)){  //判断是否为IE浏览器
                return URLEncoder.encode(filename,"UTF-8");
            }
        }
        // 其他浏览器编码格式ISO-8859-1
        return new String(filename.getBytes("UTF-8"),"ISO-8859-1");
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值