System.getProperty(" user.dir")不起作用

问题描述:

我需要从我的项目获取上传照片的相对路径,但是当我在我的测试中运行 System.getProperty(user.dir); 时,它返回 / home / user / workspace / Myproject
但是如果我从我的spring控制器运行它,它只返回 / home / user

I need to get relative path from my project to upload photos, however when I run System.getProperty("user.dir"); in my test, it returns /home/user/workspace/Myproject, but if I run it from my spring controller, it just returns /home/user.

任何人都可以提供可能正在发生的见解吗?

Can anyone provide a insight that probably is happening?

user.dir 属性是运行Java的当前工作目录。当你运行项目时,你在 / home / user / workspace / Myproject 中启动java,但是当Spring运行它时,它不能保证是相同的工作目录。您是在寻找 user.home (应该总是 / home / user )?

The user.dir property is the current working directory where you are running Java. You're starting java in /home/user/workspace/Myproject when you run your project but when Spring runs it, it's not guaranteed to be the same working directory. Are you looking for user.home (which should always be /home/user) instead?