Spring Boot 上传文件代码
Spring Boot 使用MultipartFile来完成文件上传
@ResponseBody
@RequestMapping(value = "/put")
public Boolean putFile(@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
String file_path = file.getOriginalFilename();
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(file_path)));
out.write(file.getBytes());
out.flush();
out.close();
return true;
} catch (IOException e) {
logger.error("file upload fail: " + e.getMessage());
return false;
}
} else {
logger.error("file upload fail!");
return false;
}
}