问题记录
问题场景:修改头像业务,文件上传
代码
// 上传的文件.../upload/文件.png
//在javaWeb中我们会用HttpServletRequest来获取上下文路径,当然session也可以,因为我们这里用到了session就不再多用request了
//request.getServletContext().getRealPath();
String parent = session.getServletContext().getRealPath("upload");
//使用getServletContext().getRealPath()得到的是临时文件的路径。
//每次重启服务,这个临时文件的路径还会变更。
//解决方法1:绑定为当前目录下的路径
System.out.println(parent);
// File对象指向这个路径,File是否存在
File dir = new File("parent");
每次运行项目都会生成一个tomcat文件,图片会上传到里面,去找的话可能会找不到
我的解决方法
File dir = new File("store_demo/src/main/resources/static/upload");
参考文章:Spring Boot内嵌tomcat关于getServletContext().getRealPath获取得到临时路径的问题_mb607022e25a607的技术博客_51CTO博客