简体   繁体   中英

How to connect to network drive using credential with Java?

I am reading the .txt files using the BufferedReader. But Which files I want to read that files are stored into the network drive (ex : data(\\10.10.30.50)(Z:)).

For connecting to the network drive, The drive asking for the password only, After giving the password, I am able to read the file.

How Can I connect to the network drive using Java code, I want to unlock that drive using Java code.

Use Samba(CIFS) for that purpose. I'm using JCIFS for that purpose.

NtlmPasswordAuthentication credentials = new NtlmPasswordAuthentication(domain+";"+user+":"+pass);

String fileContent(String path) {
        StringBuilder builder = null;
        try {
            SmbFile smbfile = new SmbFile(path, credentials);
            builder = readFileContent(smbfile);         

        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
}

private StringBuilder readFileContent(SmbFile sFile){
    BufferedReader reader = null;
    try{
        reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(sFile)));
    }
    catch(IOException e){
        e.printStackTrace();
    }
    StringBuilder builder = new StringBuilder();
    String in = null;
    try{
        while((in = reader.readLine())!=null){
            builder.append(in).append("\n");
        }
        reader.close();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    return builder;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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