如何从 JFileChooser 检索 UNC 路径而不是映射的驱动器路径

发布于 2024-11-02 08:29:41 字数 98 浏览 5 评论 0原文

只是想知道是否有办法从使用 JFileChooser 选择的文件返回 UNC 路径。我要选择的文件将驻留在具有 UNC 路径的映射驱动器上。现在,我似乎只能拉回映射驱动器的驱动器号。

Just wondering if there is a way to return back the UNC path from a file chosen with JFileChooser. The file that I would be selecting would reside on a mapped drive that has a UNC path. Right now, I can only seem to pull back the drive letter of a mapped drive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长安忆 2024-11-09 08:29:41

https://stackoverflow.com/users/715934/tasoocoo

我最终找到了一个解决方案执行 NET USE 命令:

 filePath = fc.getSelectedFile().getAbsolutePath();
 Runtime runTime = Runtime.getRuntime();
 Process process = runTime.exec("net use");
 InputStream inStream = process.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
 String line = null;
 String[] components = null;
 while (null != (line = bufferedReader.readLine())) {
   components = line.split("\\s+");
    if ((components.length > 2) && (components[1].equals(filePath.substring(0, 2)))) {
      filePath = filePath.replace(components[1], components[2]);
    }
 }

From https://stackoverflow.com/users/715934/tasoocoo

I ended up finding a solution that executes the NET USE command:

 filePath = fc.getSelectedFile().getAbsolutePath();
 Runtime runTime = Runtime.getRuntime();
 Process process = runTime.exec("net use");
 InputStream inStream = process.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
 String line = null;
 String[] components = null;
 while (null != (line = bufferedReader.readLine())) {
   components = line.split("\\s+");
    if ((components.length > 2) && (components[1].equals(filePath.substring(0, 2)))) {
      filePath = filePath.replace(components[1], components[2]);
    }
 }
一个人的夜不怕黑 2024-11-09 08:29:41

正如我对 Gerry 的回答的评论,ShellFolder.getDisplayName 是不可靠的,因为用户可以将显示名称更改为他们想要的任何名称。

然而,UNC 路径似乎可以通过 sun.awt.shell.ShellFolder 获得。这当然是一个“内部专有 API”,因此不能保证这将在未来版本的 java 中继续工作,但在 Windows 7 中针对 java 1.8.0_31 进行测试时,我看到一个名为 Attributes 的 ShellFolderColumnInfo 对于网络驱动器,它似乎包含 UNC 路径作为裸字符串。例如:

File networkDrive = new File("G:\");
ShellFolder shellFolder = ShellFolder.getShellFolder(networkDrive);
ShellFolderColumnInfo[] cols = shellFolder.getFolderColumns();
for (int i = 0; i < cols.length; i++) {
    if ("Attributes".equals(cols[i].getTitle())) {
        String uncPath = (String) shellFolder.getFolderColumnValue(i);
        System.err.println(uncPath);
        break; // don't need to look at other columns
    }
}

如果您在资源管理器中转到“我的电脑”,更改为“详细信息”视图并打开“网络位置”列,它似乎与我们通过 ShellFolder API 从“属性”获取的内容相匹配。不确定“属性”来自哪里,或者它在非英语语言环境中是否发生变化。

As I commented on Gerry's answer, ShellFolder.getDisplayName is unreliable because the user can changed the display name to whatever they want.

However the UNC path does seem to be available via sun.awt.shell.ShellFolder. This is of course an "internal proprietary API" so no guarantee this will continue to work in future versions of java, but testing against java 1.8.0_31 in Windows 7 I see a ShellFolderColumnInfo titled Attributes which for network drives appears to include the UNC path as a bare String. eg:

File networkDrive = new File("G:\");
ShellFolder shellFolder = ShellFolder.getShellFolder(networkDrive);
ShellFolderColumnInfo[] cols = shellFolder.getFolderColumns();
for (int i = 0; i < cols.length; i++) {
    if ("Attributes".equals(cols[i].getTitle())) {
        String uncPath = (String) shellFolder.getFolderColumnValue(i);
        System.err.println(uncPath);
        break; // don't need to look at other columns
    }
}

If you go to My Computer in explorer, change to Details view and turn on the "Network Location" column, it appears to match what we get from "Attributes" via the ShellFolder API. Not sure where "Attributes" comes from or if it changes in non-english locales.

半岛未凉 2024-11-09 08:29:41

JFileChooser 方法 getSelectedFile() 返回一个 文件,其中可能包含有用的信息。

“对于 Microsoft Windows 平台,...UNC 路径名的前缀是 "\\\\";主机名和共享名是名称序列中的前两个名称。”

The JFileChooser method getSelectedFile() returns a File, which may have helpful information.

"For Microsoft Windows platforms,…the prefix of a UNC pathname is "\\\\"; the hostname and the share name are the first two names in the name sequence."

2024-11-09 08:29:41

如果其他人正在寻找替代(我认为更简单)的解决方案,
您可以使用 ShellFolder.getDisplayName() 查找信息。例如,您可以从此处的字符串解析驱动器的网络位置:

System.out.println(ShellFolder.getShellFolder(new File(filePath.substring(0,3))).getDisplayName());

这也可能有用:

File.listRoots();

If anyone else is looking for an alternate (and I think simpler) solution,
you can find the information using ShellFolder.getDisplayName(). For example, you can parse the network location of the drive from the string here:

System.out.println(ShellFolder.getShellFolder(new File(filePath.substring(0,3))).getDisplayName());

This might also be useful:

File.listRoots();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文