Wednesday, July 16, 2014

Getting file with SMB Protocol


Get the jcifs jars from below sites..

http://mvnrepository.com/artifact/jcifs/jcifs
http://jcifs.samba.org/


If anyone wants info regarding SMB,
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365233(v=vs.85).aspx
https://www.samba.org/cifs/docs/what-is-smb.html



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




public class SMBFileReader {
private String USER_NAME = null;
private String PASSWORD = null;
private String DOMAIN = null;
private String NETWORK_FOLDER = null;


public boolean getFile(String fileName)  throws Exception{
    boolean successful = false;
    String path = null;
    NtlmPasswordAuthentication auth = null;
    SmbFile sFile = null;
    SmbFileOutputStream sfos = null;
    try {
        USER_NAME = "xyz";
        PASSWORD = "****";
        DOMAIN = "cswg.com";
        NETWORK_FOLDER = "smb://optmsdb03/WDGMD/Orders/";
        auth = new NtlmPasswordAuthentication(
                DOMAIN, USER_NAME, PASSWORD);
        path = NETWORK_FOLDER + fileName;
        sFile = new SmbFile(path, auth);
        SmbFileInputStream in = new SmbFileInputStream( sFile);
        FileOutputStream out = new FileOutputStream("C:/data/test_200.txt");
       
       
        byte[] b = new byte[8192];
        int n, tot = 0;
        long t1 = 0;
        while(( n = in.read( b )) > 0 ) {
            out.write( b, 0, n );
            tot += n;
         
        }
        in.close();
        out.close();
        successful = true;
        Log.logger("File successfully created.", "info");
    } catch (Exception e) {
        successful = false;
        Log.logger("Unable to create file. Cause: "
                + e.getMessage(), "error");
        throw e;
    }
    return successful;
}
}

No comments: