Gauri p
2 min readMar 17, 2021

Accessing shared folder using SMB

What is SMB?
SMB, which stands for Server Message Block, is a protocol for sharing files, printers, serial ports, and communications abstractions such as named pipes and mail slots between computers.

Connecting to Shared folder using smbclient
smbclient -U userid@xxx.com%password//servername/sharedFolderPath

With -U option you can specify the username and password (separated by %) in the command line itself. If you do not specify the password, you will be prompted to enter the same.

Once connected it will show the prompt as smb: \> You can type help to get the list of supported commands. For example smb: \>ls will list the contents of the shared folder.

You can also use smbclient -L \\\\servername to list the contents.

Connecting to Shared folder from Java program
Readymade SMB client libraries can be used to connect to the shared folder and perform operations like file upload, download, rename etc .

  1. smbj — https://github.com/hierynomus/smbj
  2. JCIFS — https://github.com/codelibs/jcifs

Above libraries also provide the examples on usage.

package com.example.SMBDemo;


import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;
import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.DiskShare;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFileInputStream;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import jcifs.smb.SmbFile;
import org.springframework.util.FileCopyUtils;

import java.io.*;


@SpringBootApplication
public class SmbDemoApplication {

private static final String SMB_SHARE_FOLDER = "smb://servername.xxxx.com/sharedFolder/";
private static final String SHARE_FOLDER_PATH = "sharedFolderDir";
private static final String FILE_NAME = "abc.csv";
private static final String LOCAL_DIR = "/userdata/copyfolder/";
public static void main(String[] args) {

SpringApplication.run(SmbDemoApplication.class, args);
System.out.println("SMB demo App : " );

downloadSmbFile(SMB_SHARE_FOLDER, SHARE_FOLDER_PATH, FILE_NAME, LOCAL_DIR);
printContent(SMB_SHARE_FOLDER, SHARE_FOLDER_PATH);

}


public static void printContent(String remoteUrl, String shareFolderPath)
{
try {

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("xxxx.com","username","password");

SmbFile smbfile = new SmbFile(remoteUrl + shareFolderPath + File.separator ,auth);

String a[] = smbfile.list();
for (int i = 0; i < a.length; i++) {
System.out.println("Printing list " );
System.out.println(a[i]);
}
}
catch (Exception e)
{
System.out.println("Error Creating SMBfile instance " );
System.out.println( e);
}
}

public static void downloadSmbFile(String remoteUrl, String shareFolderPath, String fileName, String localDir) {
InputStream in = null;
OutputStream out = null;
try {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("xxxx.com","username","password");

SmbFile smbfile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName,auth);
File localFile = new File(localDir + File.separator + fileName);
in = new BufferedInputStream(new SmbFileInputStream(smbfile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
FileCopyUtils.copy(in, out);
} catch (Exception e) {
System.out.println("in exception " );
e.printStackTrace();
} finally {
System.out.println("in finally " );
closeStream(in, out);
}
}
private static void closeStream(InputStream in, OutputStream out) {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Gauri p
Gauri p

Written by Gauri p

0 Followers

Passion for Solutioning

No responses yet

Write a response