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);
        }
    }
相关推荐
uzong3 小时前
技术故障复盘模版
后端
GetcharZp3 小时前
基于 Dify + 通义千问的多模态大模型 搭建发票识别 Agent
后端·llm·agent
桦说编程4 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen4 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研4 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
wyiyiyi4 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
没有bug.的程序员5 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
一只爱撸猫的程序猿5 小时前
使用Spring AI配合MCP(Model Context Protocol)构建一个"智能代码审查助手"
spring boot·aigc·ai编程
甄超锋5 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国5 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端