如何在 Java 中挂载 Windows 驱动器?

发布于 2024-07-07 13:57:46 字数 114 浏览 6 评论 0原文

我们正在使用一些通过字母(例如 f:\)访问共享驱动器的遗留代码。 使用 UNC 表示法不是一种选择。 我们的 Java 包装应用程序将作为服务运行,作为第一步,我想在代码中显式映射驱动器。 有人这样做过吗?

We are working with some legacy code that accesses a shared drive by the letter (f:\ for example). Using the UNC notation is not an option. Our Java wrapper app will run as a service, and as the first step, I would like to map the drive explicitly in the code. Has anyone done this?

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

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

发布评论

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

评论(3

宛菡 2024-07-14 13:57:46

考虑执行映射网络驱动器的 DOS 命令,如以下代码所示:

String command = "c:\\windows\\system32\\net.exe use f: \\\\machine\\share /user:user password";
Process p = Runtime.getRuntime().exec(command);
...

请参阅 net use 命令的详细信息:

The syntax of this command is:


NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
        [/USER:[domainname\]username]
        [/USER:[dotted domain name\]username]
        [/USER:[username@dotted domain name]
        [/SMARTCARD]
        [/SAVECRED]
        [[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]

Consider executing the DOS command that maps a network drive as in the following code:

String command = "c:\\windows\\system32\\net.exe use f: \\\\machine\\share /user:user password";
Process p = Runtime.getRuntime().exec(command);
...

See details on net use command:

The syntax of this command is:


NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
        [/USER:[domainname\]username]
        [/USER:[dotted domain name\]username]
        [/USER:[username@dotted domain name]
        [/SMARTCARD]
        [/SAVECRED]
        [[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]
怎会甘心 2024-07-14 13:57:46

您可以使用 JCIFS

http://jcifs.samba.org/src /docs/api/jcifs/smb/SmbFile.html

或者如果您想要更高级别的 API 并支持其他协议(如 FTP、Zip 等):

http://commons.apache.org/vfs/filesystems.html

两个选项都是纯 Java 和跨平台的。

You can use JCIFS

http://jcifs.samba.org/src/docs/api/jcifs/smb/SmbFile.html

or if you want higher level API and support for other protocols like FTP, Zip and others:

http://commons.apache.org/vfs/filesystems.html

Both options are pure Java and cross platform.

浅忆 2024-07-14 13:57:46

我认为最简单的方法是使用 Runtime.getRuntime().exec() 方法并调用“net use”命令。

例如:

    try {
        // Execute a command without arguments
        String command = "C:\\Windows\\system32\\net.exe use F: \\\\server\\share /user:user password";
        Process child = Runtime.getRuntime().exec(command);
    } catch (IOException e) {
    }

I think the easiest way is to use the Runtime.getRuntime().exec() method and call the "net use" command.

For example:

    try {
        // Execute a command without arguments
        String command = "C:\\Windows\\system32\\net.exe use F: \\\\server\\share /user:user password";
        Process child = Runtime.getRuntime().exec(command);
    } catch (IOException e) {
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文