1.文件上传功能:直接看controller
@PostMapping("/uploadFile")
public @ResponseBody String singleFileUpload(@RequestParam("file")MultipartFile file){
//判断文件是否为空
if(file.isEmpty()){
return "文件为空,上传失败!";
}
try{
//获得文件的字节流
byte[] bytes=file.getBytes();
//获得path对象,也即是文件保存的路径对象
Path path= Paths.get(FILE_DIR+file.getOriginalFilename());
//调用静态方法完成将文件写入到目标路径
Files.write(path,bytes);
return "恭喜上传成功!";
}catch (IOException e){
e.printStackTrace();
}
return "未知异常";
}
其中FILE_DIR是上传文件的路径,可以自己根据选择设置,比如我这里设置FILE_DIR="f://file//" 这个路径
2.文件上传的html页面
<form method="POST" action="/uploadFile" enctype="multipart/form-data">
<input type="file"