使用Java的文件/目录的网络路径

发布于 2024-12-08 01:54:39 字数 136 浏览 0 评论 0原文

如何使用java获取Windows PC上文件或目录的网络路径?通常我们可以在windows上的共享文件夹属性中看到它。如下图...... 在此处输入图像描述

How to get the network path of a file or directory on a windows pc using java? Usually we can see it in shared folder's properties on windows. As shown below.....
enter image description here

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

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

发布评论

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

评论(2

暮年 2024-12-15 01:54:39

使用Java,您可以在Runtime类的exec方法中传递net SHARE命令,然后从Process解析输出流类来获取目录对应的网络路径。 net SHARE Directory_name 命令的输出如下:

Share name        Share
Path              C:\Share
Remark
Maximum users     No limit
Users
Caching           Manual caching of documents
Permission        user, FULL

您需要从上面的输出中获取 Path 键的值。以下是如何实现此目的的伪代码:

    Process process = Runtime.getRuntime().exec("net SHARE directory_name");
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    StringWriter writer = new StringWriter();
    String line;

    while (null != (line = reader.readLine())) {
        writer.write(line);
    }

    System.out.println(writer.toString());

Using Java you can pass net SHARE command in exec method of Runtime class and then parse the outputstream from the Process class to get the corresponding Network Path of your directory. The output of net SHARE directory_name command is as follows:

Share name        Share
Path              C:\Share
Remark
Maximum users     No limit
Users
Caching           Manual caching of documents
Permission        user, FULL

You need to get the value of the Path key from the above output. Following is the pseudo-code of how you can achieve this:

    Process process = Runtime.getRuntime().exec("net SHARE directory_name");
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    StringWriter writer = new StringWriter();
    String line;

    while (null != (line = reader.readLine())) {
        writer.write(line);
    }

    System.out.println(writer.toString());
爱她像谁 2024-12-15 01:54:39

要调用的 API 是 WNetEnumResource。您需要使用 JNI 来调用 Windows API。可以在 http://public.m-plify.net/sourcecode/ 找到示例。

The API to call is WNetEnumResource. You need to use JNI to call Windows APIs. An example can be found at http://public.m-plify.net/sourcecode/.

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