@Controller
public class UploadController {
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "上传失败,请选择文件";
}
String fileName = file.getOriginalFilename();
String filePath = "/Users/xxx/";
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
return "上传成功";
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败!";
}
}
springboot已经配置好MultipartFile相关配置,如果需要额外配置 如最大上传文件大小:
spring:
servlet:
multipart:
max-file-size: 10MB
postman,请求模式使用form-data