项目中往往有需求,需要把用户上传的图片,视频等保存到本地,而数据库只是保存个地址,这样的话可以避免数据库响应过慢:
@Value("${web.upload-path}")
private String realPath;
// 拼接文件目录
String realpath=realPath+“icon”+"/"+“author/”+year+"/"+month+"/"+day;
File path=new File(realpath);
if(!path.exists()) {
path.mkdirs();
}
try {
// 保存文件
file.transferTo(new File(path+File.separator+(millis+files+".jpg")));
} catch (Exception e) {
// TODO: handle exception
}
我这里是按照年月日生成文件夹来保存每一天的文件,也可以按照自己的规则来保存文件
保存文件到本地
最新推荐文章于 2021-07-29 09:11:41 发布