首页 > 技术文章 > Springboot项目创建文件中相对路径问题

B-rabbit 2020-02-27 19:49 原文

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);

此时成功新建文件。

推荐阅读