Thursday, 1 August 2013

read .txt file using samba jcifs

Here i am discuss 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. To Known more visit Java CIFS Client Library


Here is Example to Read File using SMB networking protocol :-

samba.txt located in another machine contain following content:-
(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.

)


Requirement:-
1. Download jcifs-1.1.11.jar add in your lib folder

either add maven dependency in pom.xml :-


  org.samba.jcifs
  jcifs
  1.3.3


Constant.java
package com.javastoreroom.mytestapp;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    public class Constant {
 
    public static final String USER_NAME = "domain\\username"; 
    public static final String PASSWORD = "domain\\password";
    public static final String FILE_SOURCE_PATH = "smb://IP-Address/folderName/samba.txt"; // The local network's broadcast address of  target file or directory. SmbFile URLs 
    

}

SmbConnect.java
package com.javastoreroom.mytestapp;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

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

public class SmbConnect {

 private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SmbConnect.class);

 public static void main(String... args) {
  new SmbConnect().copyFiles();
 }

 public void copyFiles() {
  StringBuilder builder = null;
  try {
   NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, Constant.USER_NAME, Constant.PASSWORD); //This class stores and encrypts NTLM user credentials.
   
   SmbFile sFile = new SmbFile(Constant.FILE_SOURCE_PATH , auth); //This class represents a resource on an SMB network.

   try {
    builder = new StringBuilder();
    builder = readFileContent(sFile, builder);

    log.info("========================== display all .txt info  here ==============");
    log.info(builder.toString());
    log.info("========================== End  here ================================");

   } catch (Exception exception) {
    exception.printStackTrace();
   }
  }
  catch (Exception e) {
   e.printStackTrace();
  }
 }

 private StringBuilder readFileContent(SmbFile sFile, StringBuilder builder) {
  BufferedReader reader = null;
  try {
   reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(sFile)));
  } catch (SmbException ex) {
   Logger.getLogger(SmbConnect.class.getName()).log(Level.SEVERE, null, ex);
  } catch (MalformedURLException ex) {
   Logger.getLogger(SmbConnect.class.getName()).log(Level.SEVERE, null, ex);
  } catch (UnknownHostException ex) {
   Logger.getLogger(SmbConnect.class.getName()).log(Level.SEVERE, null, ex);
  }
  String lineReader = null;
  {
   try {
    while ((lineReader = reader.readLine()) != null) {
     builder.append(lineReader).append("\n");
    }
   } catch (IOException exception) {
    exception.printStackTrace();
   } finally {
    try {
     reader.close();
    } catch (IOException ex) {
     Logger.getLogger(SmbConnect.class.getName()).log(Level.SEVERE, null, ex);
    }
   }
  }
  return builder;
 }
}

You can also read/write, delete, make directories, rename, list contents of a directory, list the workgroups/ntdomains and servers on the network, list the shares of a server, open named pipes, authenticate web clients ...etc.

after executing SmbConnect.java following output are shown :-