前言
此篇文章用于解决一个接口内同时完成文件的上传及JSON参数的传入(生产环境已验证);
1.准备接口
import cn.cdjs.vo.UserVO;
import cn.hutool.json.JSONUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/file")
public class FileController {
    /**
     * 注意:
     *      file及vo对象需要使用@RequestPart注解
     */
    @PostMapping("/uploadFile")
    public String file(@RequestPart MultipartFile file, @RequestPart UserVO vo){
        // 重上传的文件对象中获取到的文件名称
        String fileName = file.getOriginalFilename();
        System.out.println("文件名称:" + fileName);
        // 上传的JSON数据
        String jsonParams = JSONUtil.toJsonStr(vo);
        System.out.println("JSON数据:" + jsonParams);
        return "OK!";
    }
}2.PostMan模拟前端文件上传
特别说明:
前端需使用form-data格式进行文件和JSON数据传递;

3.后端数据接收情况




















