Since the answers above, NtlmPasswordAuthentication has been deprecated.
Therefore, this is how I solved, for example, copying a file to a samba share:
String user="your_samba_username";
String pass="your_samba_password";
String path="smb://samba_ip_address/outputdir/outputfile.txt";
try {
InputStream winfis = new FileInputStream(new File("c:/inputdir/inputfile.txt"));
try {
SingletonContext baseContext = SingletonContext.getInstance();
Credentials credentials=new NtlmPasswordAuthenticator(null,user,pass);
CIFSContext testCtx = baseContext.withCredentials(credentials);
try {
SmbFile smbFile = new SmbFile(path, testCtx);
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
try {
IOUtils.copy(winfis, smbfos);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
smbfos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
} catch (CIFSException ex) {
ex.printStackTrace();
} finally {
try {
winfis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
Required libraries (dependencies):
- JCIFS Samba Protocol: [**jcifs-2.1.34.jar**][1] (SMB2 (2.02 protocol level) support, some SMB3 support)
- Bouncy Castle Provider 1.74: [**bcprov-jdk18on-1.74.jar**][2]
[1]: https://github.com/codelibs/jcifs
[2]: https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk18on/1.74
Since the accepted answer, NtlmPasswordAuthentication has been deprecated.
Therefore, this is how I solved, for example, copying a file to a samba share:
String user="your_samba_username";
String pass="your_samba_password";
String path="smb://samba_ip_address/outputdir/outputfile.txt";
try {
InputStream winfis = new FileInputStream(new File("c:/inputdir/inputfile.txt"));
try {
SingletonContext baseContext = SingletonContext.getInstance();
Credentials credentials=new NtlmPasswordAuthenticator(null,user,pass);
CIFSContext testCtx = baseContext.withCredentials(credentials);
try {
SmbFile smbFile = new SmbFile(path, testCtx);
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
try {
IOUtils.copy(winfis, smbfos);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
smbfos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
} catch (CIFSException ex) {
ex.printStackTrace();
} finally {
try {
winfis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
Required libraries (dependencies):
- JCIFS Samba Protocol: [**jcifs-2.1.34.jar**][1] (SMB2 (2.02 protocol level) support, some SMB3 support)
- Bouncy Castle Provider 1.74: [**bcprov-jdk18on-1.74.jar**][2]
[1]: https://github.com/codelibs/jcifs
[2]: https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk18on/1.74