1、创建测试类(FileController.java)
package com.jeff.controller;
import java.io.File;
import java.io.IOException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class FileController {
@PostMapping("/upload")
public String upload(MultipartFile file) {
if (file == null) {
return "上传失败,未选择文件";
}
String fileName = file.getOriginalFilename();
System.out.println("文件名:" + fileName);
String filePath = "F:\\Jeff\\project\\workspace\\mavenDemo\\src\\main\\resources\\static\\";
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
System.out.println("上传成功!");
} catch (IOException e) {
System.out.println("上传异常!" + e);
return "error";
}
return "success";
}
}
2、使用postman测试接口(未选择文件)
3、使用postman测试接口(选择文件)
4、查看项目路径
5、如果报下图错误,请查看 解决方法