Spring Boot文件上传

配置文件上传属性:

在application.properties文件中配置文件上传的属性,包括上传目录的路径、文件大小限制等。

java 复制代码
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

处理文件上传请求

上传的文件按照日期 进行归类,使用UUID给文件重命名

java 复制代码
    @PostMapping("/upload/")
    @ResponseBody
    public  Response upload(MultipartFile file) {
        // 验证是否有文件
        if(file == null || file.isEmpty()){
            return Response.newFail("Upload failed, please select file",400);
        }
        // 文件保存目录
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String format = sdf.format(new Date());
        String filePath = "D:/flies/springboot/"+format;

        // 验证文件夹
        File folder = new File(filePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        // 文件名
        String fileName = UUID.randomUUID() + file.getOriginalFilename();
        filePath = filePath  + fileName;
        File saveFile = new File(filePath);
        try {
            file.transferTo(saveFile);
            return  Response.newSuccess("Upload successful");
        } catch (IOException e) {
            e.printStackTrace();
            return  Response.newFail("Upload failed",50001);
        }
    }

文件过大

如果遇到文件过大出现413 状态码无结果

需要统一返回json,可以参考
Springboot封装统一返回结果及全局异常处理

配置文件保存路径

可以在配置中保存文件的存放位置,方便更改

配置文件

application.properties文件添加需要的配置

java 复制代码
file.path=D:\\flies\\springboot\\

@ConfigurationProperties 注解

使用注解@ConfigurationProperties将配置项和实体Bean关联起来,实现配置项和实体类字段的关联,读取配置文件数据。

java 复制代码
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "file")
public class FileConfig {
    private String path;
}

使用

获取配置信息

java 复制代码
FileConfig fileConfig = new FileConfig();
// 文件保存目录
String filePath = fileConfig.getPath();
java 复制代码
    @PostMapping("/upload/")
    @ResponseBody
    public  Response upload(MultipartFile file) {
        // 验证是否有文件
        if(file == null || file.isEmpty()){
            return Response.newFail("Upload failed, please select file",400);
        }
        FileConfig fileConfig = new FileConfig();
        // 文件保存目录
        String filePath = fileConfig.getPath();

        // 验证文件夹
        File folder = new File(filePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        // 文件名
        String fileName = UUID.randomUUID() + file.getOriginalFilename();
        filePath = filePath  + fileName;
        File saveFile = new File(filePath);
        try {
            file.transferTo(saveFile);
            return  Response.newSuccess("Upload successful");
        } catch (IOException e) {
            e.printStackTrace();
            return  Response.newFail("Upload failed",50001);
        }
    }
相关推荐
野犬寒鸦2 小时前
多级缓存架构:性能与数据一致性的平衡处理(原理及优势详解+项目实战)
java·服务器·redis·后端·缓存
帧栈4 小时前
开发避坑指南(58):Java Stream 按List元素属性分组实战指南
java
Da Da 泓4 小时前
LinkedList模拟实现
java·开发语言·数据结构·学习·算法
海琴烟Sunshine4 小时前
Leetcode 14. 最长公共前缀
java·服务器·leetcode
城管不管5 小时前
Lambda
java
龙茶清欢5 小时前
5、urbane-commerce 微服务统一依赖版本管理规范
java·运维·微服务
Tony Bai7 小时前
【Go开发者的数据库设计之道】05 落地篇:Go 语言四种数据访问方案深度对比
开发语言·数据库·后端·golang
eqwaak07 小时前
Flask实战指南:从基础到高阶的完整开发流程
开发语言·后端·python·学习·flask
海琴烟Sunshine8 小时前
Leetcode 26. 删除有序数组中的重复项
java·算法·leetcode
RoboWizard8 小时前
移动固态硬盘连接手机无法读取是什么原因?
java·spring·智能手机·电脑·金士顿