目录
(一)上传文件到本地(windows)
1.新建一个文件夹来存储上传的文件
在application.yml中写上传的文件夹地址
我这里上传到F盘下的一个文件夹
2.新建controller写接口
@RestController
public class UploadController {
//获取yml中的文件上传地址
@Value("${windows.path}")
private String filePath;
@PostMapping("/uploadFile")
public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file){
//返回map集合
Map<String, Object> map = new HashMap<String, Object>();
//判断文件名是否为空
if (file.getOriginalFilename() == null){
map.put("code",500);
map.put("message","文件名为空!");
return map;
}
try {
//获取File文件
File file1 = new File(filePath, file.getOriginalFilename());
file.transferTo(file1);
}catch (Exception e){
map.put("code",500);
map.put("message","文件服务器上传失败!");
return map;
}
map.put("code",200);
map.put("message","文件服务器上传成功!");
return map;
}
}
java.io.File类是文件和目录路径名称的抽象表示,主要用于文件和目录的创建、查找和删除等操作
我们新建一个File对象,使用里面的
public File(String parent,String child):从父路径字符串 和 子路