Java全面深入探究SpringBoot * 与文件上传
作者:铁甲小宝同学 时间:2021-11-12 20:08:24
*
* 的概念
动态拦截Actioon调用的对象,使开发者在一个Actioon执行的前后执行一段代码,也可以在Action执行前阻止其执行,同时也提供了一种可以提取Action中可重用部分代码的方式。
作用:
动态拦截Action调用的对象(也就是实际项目中的controller层的接口)
一般 * 用于对用户访问的限制。如当用户没有登录时访问主页面,则可以使用 * 进行拦截并重定向到登录页面。
* 的配置
创建interceptor 文件夹并创建LoginInterceptor Java文件且实现HandlerInterceptor 这个接口。
//必须实现HandlerInterceptor这个接口
public class LoginInterceptor implements HandlerInterceptor {
/*
* 目标方法执行以前
* */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
Object loginUser = session.getAttribute("loginUser");
if(loginUser != null) {
return true;
}
request.setAttribute("msg" ,"请登录!");
// response.sendRedirect("/");
request.getRequestDispatcher("/").forward(request,response);
return false;
}
//目标方法执行以后
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* 页面渲染以后
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
配置 *
创建 config 文件夹并创建 AdminWebConfig 文件并实现WebMvcConfigurer的addInterceptors。
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**") //静态路径也会被拦截
.excludePathPatterns("/","/login","/css/**","/fonts/**" ,"/images/**","/js/**");
}
}
* 的原理
1、根据当前请求,找到**HandlerExecutionChain【**可以处理请求的handler以及handler的所有 * 】
2、先来顺序执行 所有 * 的 preHandle方法
1、如果当前 * prehandler返回为true。则执行下一个 * 的preHandle
2、如果当前 * 返回为false。直接 倒序执行所有已经执行了的 * 的 afterCompletion;
3、如果任何一个 * 返回false。直接跳出不执行目标方法
4、所有 * 都返回True。执行目标方法
5、倒序执行所有 * 的postHandle方法。
6、前面的步骤有任何异常都会直接倒序触发 afterCompletion
7、页面成功渲染完成以后,也会倒序触发 afterCompletion
文件上传
在之前我们学习SSM的时候就知道了 文件上传 这个功能,所以在 SpringBoot中我们就不在过多介绍,原理其实都差不多。
前端文件:
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>
Controller层的配置:
@Controller
public class FileController {
@GetMapping("/updateFile")
public String FileUp(){
return "FileUp";
}
@PostMapping("/upload") // //new annotation since 4.3
public String singleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:uploadStatus";
}
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get( file.getOriginalFilename());
Files.write(path, bytes);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded '" + file.getOriginalFilename() + "'");
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/main.html";
}
}
更改文件上传大小
在application.properties中配置文件大小:
spring.servlet.multipart.max-file-size=10MB //单文件大小
spring.servlet.multipart.max-request-size=100MB //多文件
来源:https://blog.csdn.net/m0_54355125/article/details/124694558
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java毕业设计实战之在线高中考试系统的实现
![](https://img.aspxhome.com/file/2023/3/71813_0s.jpg)
java 如何计算同比增长工具类
springboot如何统一设置时区
![](https://img.aspxhome.com/file/2023/9/63659_0s.png)
Day14基础不牢地动山摇-Java基础
![](https://img.aspxhome.com/file/2023/7/67617_0s.jpg)
Java实战之在线寄查快递系统的实现
![](https://img.aspxhome.com/file/2023/2/58182_0s.png)
iOS应用中使用Toolbar工具栏方式切换视图的方法详解
![](https://img.aspxhome.com/file/2023/6/98646_0s.png)
Java 8 开发的 Mybatis 注解代码生成工具
![](https://img.aspxhome.com/file/2023/1/66211_0s.png)
Java 根据网址查询DNS/IP地址的方法
Java class文件格式之常量池_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/2/66472_0s.jpg)
Spring深入刨析声明式事务注解的源码
![](https://img.aspxhome.com/file/2023/6/61026_0s.png)
Java编程实现五子棋人人对战代码示例
![](https://img.aspxhome.com/file/2023/5/63385_0s.png)
Java11 发布前抓紧掌握这些新特性
![](https://img.aspxhome.com/file/2023/8/65578_0s.jpg)
Java源码解析之平衡二叉树
![](https://img.aspxhome.com/file/2023/2/60532_0s.png)
Android 消息机制以及handler的内存泄露
Java文本文件操作方法实例详解
Java实现堆排序和图解
![](https://img.aspxhome.com/file/2023/6/59336_0s.png)
Java NIO:浅析IO模型_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/8/65178_0s.png)
mybatis初始化SqlSessionFactory失败的几个原因分析
![](https://img.aspxhome.com/file/2023/3/62083_0s.png)
Java 由浅入深带你掌握图的遍历
![](https://img.aspxhome.com/file/2023/8/65208_0s.png)