How can I mount a windows drive in Java?

前端 未结 3 914
我在风中等你
我在风中等你 2020-11-29 06:06

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 a

相关标签:
3条回答
  • 2020-11-29 06:40

    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.

    0 讨论(0)
  • 2020-11-29 06:47

    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) {
        }
    
    0 讨论(0)
  • 2020-11-29 06:59

    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}]
    
    0 讨论(0)
提交回复
热议问题