简体   繁体   English

如何在Java中访问通用用户的My Downloads文件夹?

[英]How to access generic user's My Downloads folder in Java?

I am writing some Selenium WebDriver and AutoIt Java Tests using Chrome. 我正在使用Chrome编写一些Selenium WebDriver和AutoIt Java测试。 When I download a file in Chrome, it automatically downloads it to the Downloads folder on my computer. 当我在Chrome中下载文件时,它会自动将其下载到计算机上的“下载”文件夹中。 I am sharing this code project with multiple other users on a repository, and so I do not want to have my Downloads folder hard coded in with my specific username in it. 我正在与存储库中的多个其他用户共享此代码项目,因此我不希望我的下载文件夹中使用我的特定用户名进行硬编码。

What I mean by this is let's say the user name on my computer is "Eamon." 我的意思是,假设我计算机上的用户名是“ Eamon”。 The downloads folder on my machine would be "C:\\Users\\Eamon\\Downloads," but lets say my friend Mark downloads the project from the shared repository, his downloads folder will be located at "C:\\Users\\Mark\\Downloads," yet as he pulls from the repository to see my updated code, the download location will still be hard coded as ""C:\\Users\\Eamon\\Downloads" which will result in a "Folder does not exist" error. 我的机器上的下载文件夹是“C:\\ Users \\ Eamon \\ Downloads”,但是让我的朋友Mark从共享存储库下载项目,他的下载文件夹将位于“C:\\ Users \\ Mark \\ Downloads, “但是当他从存储库中拉出来查看我的更新代码时,下载位置仍将被硬编码为”“C:\\ Users \\ Eamon \\ Downloads”,这将导致“文件夹不存在”错误。

Is there a way in java to access the generic Downloads folder on a machine that would change based on who the user of the machine is? 在java中是否有办法访问机器上的通用下载文件夹,该文件夹将根据机器用户的身份而改变? This would help my test a lot. 这将有助于我的测试。

For a Windows machine : 对于Windows机器:

new File("C:/Users/" + System.getProperty("user.name") + "/Downloads/");

Could use similar snippets for any other operating system. 可以对任何其他操作系统使用类似的代码段。

String myHomePath= System.getProperty("user.home"); String myHomePath = System.getProperty(“user.home”);

File file = new File(myHomePath+"/Downloads/" + fileName); 文件文件=新文件(myHomePath +“/ Downloads /”+ fileName);

Note: give file name with proper extension eg foo.txt 注意:给文件名提供适当的扩展名,例如foo.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM