SpringBoot3.4.3实现文件上传和全局异常处理
SpringBoot3.4.3实现文件上传和全局异常处理

本专栏前一篇文章SpringBoot3.4.3实现(文本/附件/HTML/图片)类型邮件发送案例。今天分享SpringBoot3.4.3实现文件上传和全局异常处理功能,文件上传功能在有资料录入需求的场景经常用到。
完整代码在文章最后,如果觉得本篇文章对你有用,记得点赞、关注、收藏哦。你的支持是我持续更新的动力!
文章最后可以加入免费的Java&AI技术和支付系统沟通社群,一起探讨Java/你的产品如何与AI结合,请按照要求加入。在群中可以聊开发、系统设计、架构、行业趋势、AI等等话题
SpringBoot3专栏软件环境
- JDK17.0.12
- SpringBoot3.4.3
- IDEA2024.3.3
我们先看本篇文章对应的项目结构,请看下图

1 pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.itbeien</groupId>
<artifactId>springboot3-labs-master</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>springboot-fileupload</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2 配置信息
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
3 代码实现
package cn.itbeien.upload.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author itbeien
* 项目网站:https://www.itbeien.cn
* 公众号:贝恩聊架构
* 全网同名,欢迎小伙伴们关注
* Java/AI学习社群
* Copyright© 2025 itbeien
*/
@Controller
@Slf4j
public class UploadController {
// 定义上传文件路径
private static String UPLOADED_FOLDER = "d://temp//";
// 返回上传页面
@GetMapping("/")
public String index() {
return "upload";
}
// 处理文件上传
@PostMapping("/upload")
public String singleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
// 判断文件是否为空
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "请选择需要上传的文件");
return "redirect:uploadStatus";
}
try {
// 获取文件字节数组
byte[] bytes = file.getBytes();
// 获取文件路径
Path dir = Paths.get(UPLOADED_FOLDER);
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
// 判断路径是否存在,不存在则创建
if(!Files.exists(dir)) {
Files.createDirectories(dir);
}
// 将文件写入路径
Files.write(path, bytes);
// 添加上传成功信息
redirectAttributes.addFlashAttribute("message",
"你成功上传 '" + file.getOriginalFilename() + "'");
} catch (IOException e) {
// 添加上传失败信息
redirectAttributes.addFlashAttribute("message", "系统异常");
log.error(e.getMessage());
}
// 重定向到上传状态页面
return "redirect:/uploadStatus";
}
// 返回上传状态页面
@GetMapping("/uploadStatus")
public String uploadStatus() {
return "uploadStatus";
}
}
4 全局异常配置
package cn.itbeien.upload.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
/**
* @author itbeien
* 项目网站:https://www.itbeien.cn
* 公众号:贝恩聊架构
* 全网同名,欢迎小伙伴们关注
* Java/AI学习社群
* Copyright© 2025 itbeien
*/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
// 处理MultipartException异常
@ExceptionHandler(MultipartException.class)
public String handleError(MultipartException e, RedirectAttributes redirectAttributes) {
// 记录异常信息
log.error(e.getMessage());
// 将异常信息添加到重定向属性中
redirectAttributes.addFlashAttribute("message", e.getMessage());
// 重定向到/uploadStatus页面
return "redirect:/uploadStatus";
}
}
5 功能测试
启动项目,打开浏览器访问http://localhost:8080/进入到以下界面

当未选择上传文件,点击submit会跳转到该页面进行提示

上传文件


以上就是今天SpringBoot3.4.3实现文件上传和全局异常处理全部内容,文章最后有源码下载地址
欢迎大家关注我的项目实战内容itbeien.cn,一起学习一起进步,在项目和业务中理解各种技术。

欢迎沟通交流技术和支付业务,一起探讨聚合支付/预付卡系统业务、技术、系统架构、微服务、容器化。并结合聚合支付系统深入技术框架/微服务原理及分布式事务原理。加入我的知识星球吧

AI专栏
01IDEA&VsCode集成DeepSeek-V3 API提高编程效率
02IntelliJ IDEA集成主流 AI 编程助手及特性介绍
03Spring AI快速入门-基于DeepSeek&智谱实现聊天应用
04Spring AI中流式对话API如何使用-基于DeepSeek
06SpringAI实现角色扮演(自定义人设)和Prompts模板语法-基于DeepSeek
07LangChain4j实战-Java AI应用开源框架之LangChain4j和Spring AI
SpringBoot3专栏
01SpringBoot3专栏-SpringBoot3.4.0整合Mybatis-plus和Mybatis
02SpringBoot3.4.0结合Mybatis-plus实现动态数据源
03mapstruct对象映射在Springboot3中这样用就对了
04RocketMQ5.3.1集成SpringBoot3.4.0就这样简单
05SpringBoot3.4.0整合Redisson实现分布式锁
06MySQL增量数据同步利器Canal1.1.7环境搭建流程
07SpringBoot3.4.0集成Canal1.1.7实现MySQL实时同步数据到Redis
08基于Docker-SpringBoot3.4.0集成Apache Pulsar4.0.1实现消息发布和订阅
09SpringBoot3.4.0整合消息中间件Kafka和RabbitMQ
10SpringBoot3.4.0整合ActiveMQ6.1.4
11SpringBoot3整合Spring Security6.4.2 安全认证框架实现简单身份认证
12SpringBoot3.4.1和Spring Security6.4.2实现基于内存和MySQL的用户认证
13SpringBoot3.4.1和Spring Security6.4.2结合OAuth2实现GitHub授权登录
14SpringBoot3.4.1和Spring Security6.4.2结合JWT实现用户登录
16SpringBoot3.4.1基于MySQL8和Quartz实现定时任务管理
17SpringBoot3.4.2基于MyBatis和MySQL8多数据源使用示例
18SpringBoot3.4.3实现(文本/附件/HTML/图片)类型邮件发送案例
跟着我学微服务系列
01跟着我学微服务,什么是微服务?微服务有哪些主流解决方案?
05SpringCloudAlibaba之图文搞懂微服务核心组件在企业级支付系统中的应用
06JDK17+SpringBoot3.4.0+Netty4.1.115搭建企业级支付系统POS网关
07JDK17+SpringCloud2023.0.3搭建企业级支付系统-预付卡支付交易微服务
08JDK17+Dubbo3.3.2搭建企业级支付系统-预付卡支付交易微服务
09JDK17+SpringBoot3.3.6+Netty4.1.115实现企业级支付系统POS网关签到功能
贝恩聊架构-项目实战地址
欢迎大家一起讨论学习,加我备注"Java/AI"拉你进入Java&AI技术讨论群,备注"聚合支付"拉你进入支付系统讨论群,在技术学习、成长、工作的路上不迷路!加我后不要急,每天下午6点左右通过!营销号免入

6 源码地址
贝恩聊架构-SpringBoot3专栏系列文章、资料和源代码会同步到以下地址,代码和资料每周都会同步更新
该仓库地址主要用于存放贝恩聊架构-SpringBoot3专栏、贝恩聊架构-AI专栏、基于企业级支付系统学习微服务整体技术栈所有资料和源码
