Reputation: 13
I can to write a text file from remote pc which contain a share folder without any authentication. But when I try to read file from a share folder with authentication it cannot read the file.the actual file location is
url : 10.11.201.45
Drive : D
username :BELY
password : BELY-du
file location : D://Share_BELY/bb/txt.
Here's my code:
// The name of the file to open.
String fileName = "//10.11.201.45/D$/Share-BELY/BELY:BELY-du@/bb.txt";
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
Upvotes: 1
Views: 6064
Reputation: 10069
Using JCIFS library we can achieve that.
Sample Code :
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");
SmbFile smbFile = new SmbFile("//path", auth);
InputStream in;
if (smbFile.exists()) {
in = smbFile.getInputStream();
}
Upvotes: 4
Reputation: 1617
Various solutions using different libraries
Write I/O file to shared network drive using credentials
or
Read remote file in java which needs username and password
or
Upvotes: 1