一、依赖引入 / 配置文件编写

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>    
server:
  port: 8009
  servlet:
    encoding:
      charset: utf-8
spring:
  servlet:
    multipart:
      #文件上传最大大小 1048576是1mb
      maxFileSize: 10485760
      #整个请求最大大小
      maxRequestSize: 104857600
  mvc:
    hiddenmethod:
      filter:
        #设置一下mvc配置,不然会报错
        enabled: true

二、前后端分离,前端html

<html>

<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
    <script rel="stylesheet" src="../static/jquery-1.11.0.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="../static/bootstrap.min.css" type="text/css"/>
</head>
<body>
<center>
    <table border="1" width="600px">
        <tr>
            <td>文件:</td>
            <td><input type="file" id="bigHeadImg" name="bigHeadImg"></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button type="submit" id="submit">上传</button>
                </center>
            </td>
        </tr>
    </table>
</center>
<script>

    $("#submit").click(function () {
        var files = $('#bigHeadImg').prop('files');
        var data = new FormData();
        data.append('bigHeadImg', files[0]);

        $.ajax({
            type: 'POST',
            url: "http://localhost:8009/file/uploadFile",
            data: data,
            cache: false,
            processData: false,
            contentType: false,
            success: function (ret) {
                alert(ret);
            }
        });
    });
</script>

</body>

</html>

三、FileController

 1 package com.sdkj.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.CrossOrigin;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 import org.springframework.web.multipart.MultipartFile;
 8 
 9 import java.io.File;
10 import java.io.IOException;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import java.util.UUID;
14 
15 /**
16  * @Author wangshuo
17  * @Date 2022/5/18, 17:40
18  * Please add a comment
19  */
20 @Controller
21 @RequestMapping(value = "/file")
22 @CrossOrigin(origins = {"*"}, allowCredentials = "true")
23 public class FileController {
24 
25     @RequestMapping(value = "/uploadFile")
26     @ResponseBody
27     public String uploadFail(MultipartFile bigHeadImg) throws IOException {
28 
29         //获取后缀名
30         String lastName = bigHeadImg.getOriginalFilename().substring(bigHeadImg.getOriginalFilename().lastIndexOf("."));
31         //分日期存放
32         Date date = new Date();
33         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
34         String formatDate = simpleDateFormat.format(date);
35         //文件转存
36         String dir = "D:\\file-upload-warehouse\\" + formatDate;
37         File file = new File(dir + "\\" + UUID.randomUUID().toString() + lastName);
38         //文件夹操作
39         if (!file.exists())
40             file.mkdirs();
41         //直接将文件存放到指定目录
42         bigHeadImg.transferTo(file);
43         return "success";
44     }
45 }