## 一、单文件上传 ```java import cn.doooog.[project].po.ResponseResult; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.Map; @RestController @RequestMapping("/api/upload") public class FileController { @PostMapping("/file") public ResponseResult> uploadFile(@RequestBody MultipartFile file) { if (file.isEmpty()) return new ResponseResult<>(203, "文件不能为空", null); try { String fileName = file.getOriginalFilename() + System.currentTimeMillis(); String savePath = "bin/upload/"; File saveFile = new File(savePath + fileName); if (!saveFile.getParentFile().exists()) saveFile.getParentFile().mkdirs(); file.transferTo(new File(savePath + fileName)); return new ResponseResult<>(200, "上传成功", Map.of("fileName", fileName, "savePath", savePath)); } catch (IOException e) { e.printStackTrace(); return new ResponseResult<>(500, "上传失败", Map.of("error", e.getMessage())); } } } ``` ## 二、多文件上传 直接嵌套使用单文件上传,减少重复代码 ```java @PostMapping("/files") public ResponseResult>> uploadFiles(@RequestBody MultipartFile[] files) { if (files.length == 0) return new ResponseResult<>(203, "文件列表不能为空", null); List> list = new java.util.ArrayList<>(); for (MultipartFile file : files) { ResponseResult> result = uploadFile(file); list.add(result.getData()); } return new ResponseResult<>(200, "文件上传完成", list); } ``` ## 三、限制文件大小 创建`MultipartConfig.java`配置类 ```java import jakarta.servlet.MultipartConfigElement; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.unit.DataSize; @Configuration public class MultipartConfig { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // 单个文件最大10MB factory.setMaxFileSize(DataSize.ofMegabytes(10)); // 设置总上传数据总大小20MB factory.setMaxRequestSize(DataSize.ofMegabytes(20)); return factory.createMultipartConfig(); } } ``` ## 四、限制文件类型 ```java @PostMapping("/img") public ResponseResult> uploadFile(@RequestBody MultipartFile file) { if (file.isEmpty()) return new ResponseResult<>(203, "文件不能为空", null); // 检查文件类型 String contentType = file.getContentType(); if (!"image/jpeg".equals(contentType) && !"image/png".equals(contentType)) { return new ResponseResult<>(203, "只允许上传jpeg和png格式文件", null); } try { String fileName = file.getOriginalFilename() + System.currentTimeMillis(); String savePath = "bin/upload/"; File saveFile = new File(savePath + fileName); if (!saveFile.getParentFile().exists()) saveFile.getParentFile().mkdirs(); file.transferTo(new File(savePath + fileName)); return new ResponseResult<>(200, "上传成功", Map.of("fileName", fileName, "savePath", savePath)); } catch (IOException e) { e.printStackTrace(); return new ResponseResult<>(500, "上传失败", Map.of("error", e.getMessage())); } } ``` Loading... ## 一、单文件上传 ```java import cn.doooog.[project].po.ResponseResult; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.Map; @RestController @RequestMapping("/api/upload") public class FileController { @PostMapping("/file") public ResponseResult<Map<String,String>> uploadFile(@RequestBody MultipartFile file) { if (file.isEmpty()) return new ResponseResult<>(203, "文件不能为空", null); try { String fileName = file.getOriginalFilename() + System.currentTimeMillis(); String savePath = "bin/upload/"; File saveFile = new File(savePath + fileName); if (!saveFile.getParentFile().exists()) saveFile.getParentFile().mkdirs(); file.transferTo(new File(savePath + fileName)); return new ResponseResult<>(200, "上传成功", Map.of("fileName", fileName, "savePath", savePath)); } catch (IOException e) { e.printStackTrace(); return new ResponseResult<>(500, "上传失败", Map.of("error", e.getMessage())); } } } ``` ## 二、多文件上传 直接嵌套使用单文件上传,减少重复代码 ```java @PostMapping("/files") public ResponseResult<List<Map<String,String>>> uploadFiles(@RequestBody MultipartFile[] files) { if (files.length == 0) return new ResponseResult<>(203, "文件列表不能为空", null); List<Map<String,String>> list = new java.util.ArrayList<>(); for (MultipartFile file : files) { ResponseResult<Map<String,String>> result = uploadFile(file); list.add(result.getData()); } return new ResponseResult<>(200, "文件上传完成", list); } ``` ## 三、限制文件大小 创建`MultipartConfig.java`配置类 ```java import jakarta.servlet.MultipartConfigElement; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.unit.DataSize; @Configuration public class MultipartConfig { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // 单个文件最大10MB factory.setMaxFileSize(DataSize.ofMegabytes(10)); // 设置总上传数据总大小20MB factory.setMaxRequestSize(DataSize.ofMegabytes(20)); return factory.createMultipartConfig(); } } ``` ## 四、限制文件类型 ```java @PostMapping("/img") public ResponseResult<Map<String,String>> uploadFile(@RequestBody MultipartFile file) { if (file.isEmpty()) return new ResponseResult<>(203, "文件不能为空", null); // 检查文件类型 String contentType = file.getContentType(); if (!"image/jpeg".equals(contentType) && !"image/png".equals(contentType)) { return new ResponseResult<>(203, "只允许上传jpeg和png格式文件", null); } try { String fileName = file.getOriginalFilename() + System.currentTimeMillis(); String savePath = "bin/upload/"; File saveFile = new File(savePath + fileName); if (!saveFile.getParentFile().exists()) saveFile.getParentFile().mkdirs(); file.transferTo(new File(savePath + fileName)); return new ResponseResult<>(200, "上传成功", Map.of("fileName", fileName, "savePath", savePath)); } catch (IOException e) { e.printStackTrace(); return new ResponseResult<>(500, "上传失败", Map.of("error", e.getMessage())); } } ``` 最后修改:2025 年 11 月 19 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 👆请简单粗暴地爱我。