Access to Windows shared directory with username and password protection in Java programs posted on 2015-11-20 14:03 Cloud self-water self-free reading (3744) Comments (0) Edit Favorites Accessing a directory with full read and write permissions in a Java program is relatively straightforward and does not differ from the normal directory.
But to access a directory that requires user and password authentication requires a little bit of finesse.
The introduction of an open Source library makes it easier to implement this requirement.
1. Download library file: https://jcifs.samba.org/The downloaded zip file contains not only the jar files, but also documentation and examples.
2. Copy the Jcif-1.3.18.jar into the classpath.
3. code example:
1String User="Your_user_name";
2String Pass="Your_pass_word";
3
4String SharedFolder="Shared";
5String Path="smb://ip_address/"+SharedFolder+"/test.txt";
6Ntlmpasswordauthentication Auth=NewNtlmpasswordauthentication ("", user, pass);
7Smbfile Smbfile=NewSmbfile (Path,auth);
8 Smbfileoutputstream smbfos = new smbfileoutputstream (Smbfile);
9 smbfos.write ( "testing.and writing to a file ".getbytes ());
10 System.out.println ( "completed nice !< Span style= "color: #000000;" > "); Note: If you have a shared directory, for example: \\192.168.1.2\testdir\
So the path to SMB is: smb://192.168.1.2/testdir/
ntlmpasswordauthentication requires three parameters, the first is the domain name, no, fill null, the second is the user name, the third is a password
After getting smbfile, the operation is basically the same as the java.io.File.
There are other features such as:
Smbfile.copyto
Smbfile.renameto
Wait a minute
Accessing a shared directory with user name and password protection in a Java program