SpringMVC框架提供了MultipartFile对象,该对象表示上传的文件,要求变量名称必须和表单file标签的name属性值相同
下面是我的目录结构:
4.0.0
com.ayit
springmvc-03
1.0-SNAPSHOT
war
springmvc-03 Maven Webapp
http://www.example.com
org.springframework
spring-webmvc
5.3.10
javax.servlet
jsp-api
2.0
commons-fileupload
commons-fileupload
1.4
Archetype Created Web Application
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
dispatcherServlet
/
Title
package com.ayit.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("upload")
public String upload(MultipartFile file, Model model){//MultipartFile的变量名和type=file表单中name属性的属性值一致
//获取上传的文件的名称
String uploadFileName = file.getOriginalFilename();
//使用UUID作为文件名的前缀,防止文件名冲突
uploadFileName = UUID.randomUUID().toString().replace("-","") + uploadFileName;
//指定上传路径
String uploadPath = "E:\\apache-tomcat-8.5.31\\webapps\\upload\\";
//开始上传
try {
file.transferTo(new File(uploadPath,uploadFileName));
} catch (IOException e) {
e.printStackTrace();
}
//保存文件名
model.addAttribute("uploadFileName",uploadFileName);
//跳转页面
return "/list.jsp";
}
}
<%--
Created by IntelliJ IDEA.
User: Mriacles21
Date: 2021/10/21
Time: 19:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
Title
list.jsp
启动存放图片资源的服务器
启动idea中配置的tomcat服务器,输入网址,开始访问资源
选择要上传的图片,点击打开
点击上传:
Controller调用service,service调用dao,异常都是向上抛出的,最终有DispatcherServlet找异常处理器进行异常的处理
package com.qf.exception;
public class MyException extends Exception {
public MyException(String message){
super(message);
}
}
package com.qf.exception;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(
HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {
ModelAndView modelAndView = new ModelAndView();
if (ex instanceof NullPointerException){
modelAndView.addObject("msg","空指针异常");
}else if(ex instanceof MyException){
modelAndView.addObject("msg",ex.getMessage());
}else {
modelAndView.addObject("msg","其他异常");
}
modelAndView.setViewName("/error.jsp");
return modelAndView;
}
}
@RequestMapping("testException")
//测试异常方法
public void testException() throws MyException {
//空指针异常
// String str = null;
// str.length();
throw new MyException("自定义异常");
}
拦截器的概述
SpringMVC框架中的拦截器用于对处理器进行预处理和后处理的技术。可以定义拦截器链,连接器链就是将拦截器按着一定的顺序结成一条链,在访问被拦截的方法时,拦截器链中的拦截器会按照定义的顺序执行。
拦截器和过滤器的功能比较类似,有以下区别:
过滤器是Servlet规范的一部分,任何框架都可以使用过滤器技术。
拦截器是SpringMVC框架独有的
过滤器配置了/*,可以拦截任何资源
拦截器只会对控制器中的方法进行拦截
拦截器也是AOP思想的一种实现方式,需要实现HandlerInterceptor接口
package com.qf.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
//在调用controller方法之前执行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle:调用controller方法之前执行!");
return true;//true表示继续向下执行,false表示不想下执行
}
//在调用controller方法之后执行,(发生异常不执行)
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle:调用controller方法之后执行!");
}
//最后执行
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion:最后执行!");
}
}
@RequestMapping("testInterceptor")
public String testInterceptor(){
System.out.println("testInterceptor...");
return "/list.jsp";
}