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

时间:2023-11-28 09:19:32

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

原代码:

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

File file = new File(location);

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

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

解决方法

指明项目路径:

System.getProperty("user.dir")

将上面改为

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

File file = new File(location);

此时成功新建文件。