spring boot 上传文件到指定目录

方法
/*
接受的
spring boot 上传文件到指定目录
/

@RequestMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String realPath ="指定的文件存储路径"
String format = sdf.format(new Date());
File folder = new File(format+realPath);
if(!folder.isDirectory()){
folder.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
Long sixe=uploadFile.getSize();
String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."),oldName.length());
try{
uploadFile.transferTo(new File(folder,newName));
String filePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+"/uploadFile"+format+newName ;
return filePath ;
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败" ;
}

猜你喜欢

转载自blog.51cto.com/15084467/2600680