Accessing a shared directory with user name and password protection in a Java program

Source: Internet
Author: User
Tags password protection

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.