Come posso montare un'unità Windows in Java?
-
03-07-2019 - |
Domanda
Stiamo lavorando con alcuni codici legacy che accedono a un'unità condivisa dalla lettera (f: \ per esempio). L'uso della notazione UNC non è un'opzione. La nostra app wrapper Java verrà eseguita come servizio e, come primo passo, vorrei mappare esplicitamente l'unità nel codice. Qualcuno l'ha fatto?
Soluzione
Prendi in considerazione l'esecuzione del comando DOS che mappa un'unità di rete come nel seguente codice:
String command = "c:\\windows\\system32\\net.exe use f: \\\\machine\\share /user:user password";
Process p = Runtime.getRuntime().exec(command);
...
Vedi i dettagli sul comando 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}]
Altri suggerimenti
Puoi usare JCIFS
http://jcifs.samba.org/src/ docs / api / JCIFS / SMB / SmbFile.html
o se desideri API di livello superiore e supporto per altri protocolli come FTP, Zip e altri:
http://commons.apache.org/vfs/filesystems.html
Entrambe le opzioni sono Java puro e multipiattaforma.
Penso che il modo più semplice sia usare il metodo Runtime.getRuntime (). exec () e chiamare il "quot use net" " comando.
Ad esempio:
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) {
}