[英]How to connect to network drive using credential with Java?
我正在使用BufferedReader讀取.txt文件。 但是我想要讀取哪些文件存儲到網絡驅動器中(例如:data(\\ 10.10.30.50)(Z :))。
對於連接到網絡驅動器,驅動器只詢問密碼,在給出密碼后,我能夠讀取文件。
如何使用Java代碼連接到網絡驅動器,我想使用Java代碼解鎖該驅動器。
為此目的使用Samba(CIFS)。 我正在為此目的使用JCIFS。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.