# 作用:
1. 在指定的方法调用前后执行预先设定后的的代码
2. 阻止原始方法的执行
# 核心原理:
AOP思想
# 拦截器链:
多个拦截器按照一定的顺序,对原始被调用功能进行增强
/**
* 三个方法的运行顺序为 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");
}
}
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/m8"/>
<bean class="com.xinzhi.intercepter.MyInterceptor"/>
mvc:interceptor>
mvc:interceptors>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<mvc:mapping path="/**"/>
<mvc:mapping path="/handleRun*"/>
<mvc:exclude-mapping path="/b*"/>
<bean class="MyInterceptor"/>
mvc:interceptor>
mvc:interceptors>
1)日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。
2)权限检查:如登录检测,进入处理器检测是否登录,如果没有直接返回到登录页面;
3)性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反向代理,如apache可以自动记录);
@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>
1 导入maven依赖
commons-fileupload commons-fileupload 1.42 前端页面
<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");
}
}