Springboot项目创建文件中相对路径问题


Springboot项目创建文件中相对路径问题

原代码:

String location = "./src/main/resources/UsersFiles/" + userId + "/";

File file = new File(location);

上面代码在没有tomcat环境下可以正常使用。当如果在tomcat之下。就会在所需的路径之前加上一个用户的/private路径。

3wbSfS.png

解决方法

指明项目路径:

System.getProperty("user.dir")

将上面改为

String location = System.getProperty("user.id") + "/src/main/resources/UsersFiles/" + userId + "/";

File file = new File(location);

此时成功新建文件。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM