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 "上传失败" ;
}
更多相关文章
- PHP如何删除一个路径下的所有文件夹和文件?
- PHP读取Excel图片对象,并保存替换为相对路径
- php命令行下相对路径问题的解决方法
- PHP获取绝对路径dirname(__FILE__)和__DIR__比较
- 简单的路径规划案例分享
- 算法:有向无环图的最短路径
- XHTML 相对路径与绝对路径
- CSS文件filemtime没有调用路径两次