简体   繁体   English

如何在Java中安装Windows驱动器?

[英]How can I mount a windows drive in Java?

We are working with some legacy code that accesses a shared drive by the letter (f:\\ for example). 我们正在处理一些通过字母访问共享驱动器的遗留代码(例如f:\\)。 Using the UNC notation is not an option. 使用UNC表示法不是一种选择。 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. 我们的Java包装器应用程序将作为服务运行,作为第一步,我想在代码中显式映射驱动器。 Has anyone done this? 有没有人这样做过?

Consider executing the DOS command that maps a network drive as in the following code: 考虑执行映射网络驱动器的DOS命令,如以下代码所示:

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: 查看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}]

You can use JCIFS 您可以使用JCIFS

http://jcifs.samba.org/src/docs/api/jcifs/smb/SmbFile.html 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: 或者如果您需要更高级别的API并支持其他协议,如FTP,Zip等:

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

Both options are pure Java and cross platform. 这两个选项都是纯Java和跨平台。

I think the easiest way is to use the Runtime.getRuntime().exec() method and call the "net use" command. 我认为最简单的方法是使用Runtime.getRuntime()。exec()方法并调用“net use”命令。

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) {
    }

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

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