JCIFS Example Functions

Posted: June 25, 2009 in Java, Programming
Tags:

jcifs

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.

Source :
http://jcifs.samba.org/

package ftpjcifs;

import java.util.LinkedList;
import jcifs.UniAddress;
import jcifs.smb.*;

/**
*
* @author Reza
*/
public class SmbFunction
{
private UniAddress domain;
private NtlmPasswordAuthentication authentication;

public SmbFunction()
{

}

/**
*
* @param address
* @param username
* @param password
* @throws java.lang.Exception
*/
public void login(String address, String username, String password) throws Exception
{

setDomain(UniAddress.getByName(address));
setAuthentication(new NtlmPasswordAuthentication(address, username, password));
SmbSession.logon(getDomain(), authentication);

}

/**
*
* @param path
* @return
* @throws java.lang.Exception
*/
public LinkedList getList(String path) throws Exception
{
LinkedList fList = new LinkedList();
SmbFile f = new SmbFile(path,authentication);
SmbFile[] fArr = f.listFiles();

for(int a = 0; a < fArr.length; a++) { fList.add(fArr[a].getName()); System.out.println(fArr[a].getName()); } return fList; } /** * * @param path * @return * @throws java.lang.Exception */ public boolean checkDirectory(String path) throws Exception { if(!isExist(path)) { System.out.println(path + " not exist"); return false; } if(!isDir(path)) { System.out.println(path + " not a directory"); return false; } return true; } /** * * @param path * @return * @throws java.lang.Exception */ public boolean isExist(String path) throws Exception { SmbFile sFile = new SmbFile(path, authentication); return sFile.exists(); } /** * * @param path * @return * @throws java.lang.Exception */ public boolean isDir(String path) throws Exception { SmbFile sFile = new SmbFile(path, authentication); return sFile.isDirectory(); } /** * * @param path * @throws java.lang.Exception */ public void createDir(String path) throws Exception { SmbFile sFile = new SmbFile(path, authentication); sFile.mkdir(); } /** * * @param path * @throws java.lang.Exception */ public void delete(String path) throws Exception { SmbFile sFile = new SmbFile(path, authentication); sFile.delete(); } /** * * @param path * @return * @throws java.lang.Exception */ public long size(String path) throws Exception { SmbFile sFile = new SmbFile(path, authentication); return sFile.length(); } /** * * @param path * @return * @throws java.lang.Exception */ public String getFileName(String path) throws Exception { SmbFile sFile = new SmbFile(path, authentication); return sFile.getName(); } /** * @return the domain */ public UniAddress getDomain() { return domain; } /** * @param domain the domain to set */ public void setDomain(UniAddress domain) { this.domain = domain; } /** * @return the authentication */ public NtlmPasswordAuthentication getAuthentication() { return authentication; } /** * @param authentication the authentication to set */ public void setAuthentication(NtlmPasswordAuthentication authentication) { this.authentication = authentication; } } [/sourcecode]

Comments
  1. Harris says:

    Great tutorial. Thank you.

  2. g says:

    Really useful.

  3. Qudrath says:

    Is there any way to authenticate only once.. If we authenticate multiple times it effects performace issue. if we do SmbFile s = New SmbFile(path, authentication) multipls times, each time when it hit folders it will check the login credentiials.

  4. Hi Friends

    I have problem read xml when i m using this api means Smbfile because when i parse that xml object then it gives error so please any one gives me solution for this things this is argent so please if any one know solution then please reply me.
    Second thing i want to say that i have used .dtd file in xml it gives error for this dtd so what will be solution for this task.
    i m creating one s/w for eCTD viewer like GECCO but GECCO is desktop application and i m converting that application to web application so please give solution for this.

    Thanks
    Bipin

  5. kyoayala says:

    It was very useful for me. Thanks Azer. See you later.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s