Thursday, September 29, 2011

JCIFS

The Java CIFS Client Library

JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java. CIFS is the standard file sharing protocol on the Microsoft Windows platform (e.g. Map Network Drive ...). This client is used extensively in production on large Intranets.

1)Download JCIFS using below URL
http://jcifs.samba.org/src/jcifs-1.3.16.zip

2)One sample program for accessing a file of other systems which is in network and writing something on that.

 Logon.java
------------------------------------------------------

import java.io.FileOutputStream;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

public class Logon {
    public static void main( String argv[] ) throws Exception {
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("ISSHYD.COM", "isshyd\\kirankumar.g", "indiangk1791$");
             String path = "smb://192.168.1.209/D$/file1.txt";
        SmbFile sFile = new SmbFile(path, auth);
             SmbFileOutputStream sfos = new SmbFileOutputStream(sFile,true);
            sfos.write("\n append-kiran6".getBytes());
        System.out.println("Done");
      
        // sFile.delete();
    }
}

Note :
Add jcifs.jar in build path before running this program
In this program,
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", "username", "password");

ISSHYD.COM----------->is domaine name of system to access(MyComputer-->Right click-->then see once your domain name of system)(giving domain name is not necessary you can give empty "" )
isshyd\\kirankumar.g-------->is username of system to access
indiangk1791$------------->is password of system to access.
192.168.1.209-------------->IP Address of system to access.

in this program i am accessing file1.txt file in D drive of System and wrote some thing on that file.

Note2:once  see jcifs home/examples folder sample programs.

3)Another Sample program to access all the folders and files of a specific directory.

Here i am accessing all the files and folders of  D drive.

Sample1.java
---------------------------------------------------------------

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

public class Sample1 {
    public static void main( String argv[] ) throws Exception {
        //String user = "ISSHYD\\kirankumar.g:indiangk1791$";
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domaine", "username", "password");
             String path = "smb://192.168.1.214/D$/";
        SmbFile sFile = new SmbFile(path, auth);
              SmbFile[] files = sFile.listFiles();
        System.out.println("files");
       
        for (SmbFile x : files) {
            System.out.println("file: " + x + ".  path: " + x.getPath() + ".  isDirectory? " + x.isDirectory());
           
       
            if (x.isDirectory()) {
                for (SmbFile dir : x.listFiles()) {
                    System.out.println("dir file: " + dir + ".  path: " + dir.getPath() + ".  isDirectory? " + dir.isDirectory());
                }
            }
        }
    }
}


5 comments:

  1. How would you do this on an android applicaion?

    ReplyDelete
  2. Hi. How would you make an IPP print job on a NTLM network? I get the following error when I try to send document to printserver:

    nov 05, 2014 3:15:31 PM org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
    INFO: ntlm authentication scheme selected
    nov 05, 2014 3:15:31 PM org.apache.commons.httpclient.HttpMethodDirector authenticate
    SEVERE: Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials
    org.apache.commons.httpclient.auth.InvalidCredentialsException: Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials
    at no.klp.ecm.followme.JCIFS_NTLMScheme.authenticate(JCIFS_NTLMScheme.java:129)
    at org.apache.commons.httpclient.HttpMethodDirector.authenticateHost(HttpMethodDirector.java:281)
    at org.apache.commons.httpclient.HttpMethodDirector.authenticate(HttpMethodDirector.java:233)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:169)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)

    ReplyDelete
  3. Do we need to share folder to access?

    ReplyDelete





  4. NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "user",
    "pass");
    String path = "smb://IP/E$/";
    SmbFile sFile = new SmbFile(path, auth);
    SmbFile[] files = sFile.listFiles();
    System.out.println("files");


    But still return "Exception in thread "main" jcifs.smb.SmbAuthException: Access is denied".Do I need to share E drive?.Please give me solution.

    ReplyDelete