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

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

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

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

查看net use命令的详细信息:

该命令的语法是:


 NET使用
 [devicename |  *] [\\ computername \ sharename [\ volume] [密码|  *]
         [/ USER:[域名\]用户名]
         [/ USER:[点缀域名\]用户名]
         [/ USER:[用户名@点名域名]
         [/智能卡]
         [/ SAVECRED]
         [[/ DELETE] |  [/ PERSISTENT:{YES | 没有}]]

 NET USE {devicename |  *} [密码|  *] / HOME

 NET使用[/ PERSISTENT:{YES | 没有}]

您可以使用JCIFS

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

或者如果您需要更高级别的API并支持其他协议,如FTP,Zip等:

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

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

我认为最简单的方法是使用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) { }