I have a question about accessing file with Samba JCIFS.
So there is a server I want to access, let\'s call it server.unv.edu and the workgroup is WKGRP.
There
You are making this harder than it should be. Please follow the below steps and make sure the shared folder you are creating has write access for this user you are using.
I was running this on Linux and wanted to write to a Windows box so you want to create a shared folder and put the shared folder name in the below variable if you don't know how to create shared folder on windows ...use google as always
String user = "your_user_name";
String pass ="your_pass_word";
String sharedFolder="shared";
String path="smb://ip_address/"+sharedFolder+"/test.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
SmbFile smbFile = new SmbFile(path,auth);
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
smbfos.write("testing....and writing to a file".getBytes());
System.out.println("completed ...nice !");
/** You can simply use this util class for read write operations. **/
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class NDMUtil {
public static void writeToNDM(String userName, String password, String domain, String sharedPath,
String textToWrite) throws MalformedURLException, SmbException, UnknownHostException, IOException {
NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication(domain, userName, password);
try (OutputStream out = new SmbFileOutputStream(new SmbFile(sharedPath, authentication))) {
byte[] bytesToWrite = textToWrite.getBytes();
if (out != null && bytesToWrite != null && bytesToWrite.length > 0) {
out.write(bytesToWrite);
}
}
;
}
public static String readFromNDM(String userName, String password, String domain, String sharedPath)
throws MalformedURLException, SmbException, IOException {
NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication(domain, userName, password);
String fileContent = IOUtil.toString(new SmbFileInputStream(new SmbFile(sharedPath, authentication)),
StandardCharsets.UTF_8.name());
System.out.println(fileContent);
return fileContent;
}
}