Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write I/O file to shared network drive using credentials

I want to drop a .txt file on a shared network drive. The path is a map on a networkdrive which requires credentials (login and password). Can i pass these parameters using FileOutputStream?

FileOutputStream fos;
DataOutputStream dos;

try {
    File file= new File(path + "/" + fileName + ".txt");
    fos = new FileOutputStream(file);
    dos=new DataOutputStream(fos);
    dos.writeChars(stringContent);
    dos.close();
    fos.close();
}
catch(IOException eio){
}

Thank you.

like image 394
Anonymoose Avatar asked May 15 '12 11:05

Anonymoose


1 Answers

No. Use java CIFS Client library. you can connect remote windows machine through java. example -

String user = "user:password";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
String path = "smb://my_machine_name/D/MyDev/test.txt";
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write("Test".getBytes());
sfos.close();

Thanks

EDIT: JCIFS only supports the unsecure SMB1 protocol and has been in maintainance mode for some years. Use jcifs-ng for SMB2/SMB3 support which is required from Windows 10.

like image 108
Subhrajyoti Majumder Avatar answered Oct 22 '22 12:10

Subhrajyoti Majumder