如何在 Java 中安装 Windows 驱动器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/208839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I mount a windows drive in Java?
提问by Brett McCann
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?
我们正在处理一些通过字母(例如 f:\)访问共享驱动器的旧代码。使用 UNC 表示法不是一种选择。我们的 Java 包装器应用程序将作为服务运行,作为第一步,我想在代码中显式映射驱动器。有没有人做过这个?
采纳答案by Jorge Ferreira
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}]
回答by Jonas K
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) {
}
回答by ddimitrov
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 和跨平台的。