-1

I tried below code for access network shared directory.But it worked if credentials not needed.

Once i have to pass the credentials it may be worked.

But i don't how to pass credentials for sharedDrive in following code.

File f = new File("//hostname/sharedDrive/Folder");

         // returns pathnames for files and directory
File[]  paths = f.listFiles();

Anyone suggest me way to access folder with credentials in java without using external libraries?

Mister X
  • 3,406
  • 3
  • 31
  • 72
  • Possible duplicate https://stackoverflow.com/questions/11724576/read-remote-file-in-java-which-needs-username-and-password – Sajith Silva Sep 07 '17 at 04:44
  • @SajithSilva,I need to list out files from folder but that link contains passing filename added with folder specification.So it's not duplicate – Mister X Sep 07 '17 at 04:46

1 Answers1

0

use with constructor for NtlmPasswordAuthentication

NtlmPasswordAuthentication ntlmAuthentication =
       NtlmPasswordAuthentication(domain, username, password);
SmbFile newFile = new SmbFile("smb://hostname/sharedDrive/Folder",ntlmAuthentication);

or directly

SmbFile newFile = new SmbFile("smb://hostname/sharedDrive/Folder",
       new NtlmPasswordAuthentication("", username, password));

or (if no password needed)

SmbFile newFile = new SmbFile("smb://hostname/sharedDrive/Folder",
       NtlmPasswordAuthentication.ANONYMOUS);
Gogu CelMare
  • 699
  • 6
  • 15