planning to read a file over a Windows from Ubuntu in Java using jcifs.Tried a simple approach using:
String user = "mydomain;myuser:mypassword";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
SmbFile remotefile = new SmbFile("smb://myserver/myfolder/myfile.jar",auth);
Knowing that the server works and the login values are correct,all i get is a logon failure,what could be the problem here?
Not sure if you got this to work.
But after much pain and anguish, I figured the NtlmPasswordAuthentication
call must include the domain.
So if you're using the code @user717630 posted, you'll just have to change the NtlmPasswordAuthentication
call to:
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("mydomain",user, pass);
The following program authenticates and writes a file on the protected share folder:
import java.util.Properties;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
public class ProtectFolderTest {
private String USER_NAME = null;
private String PASSWORD = null;
private String DOMAIN = null;
private String NETWORK_FOLDER = null;
public static void main(String args[]) {
try {
String fileContent = "Hi, This is the SmbFile.";
new ProtectFolderTest().copyFiles(fileContent, "SmbFile1.text");
} catch (Exception e) {
System.err.println("Exception caught. Cause: " + e.getMessage());
}
}
public boolean copyFiles(String fileContent, String fileName) {
boolean successful = false;
String path = null;
NtlmPasswordAuthentication auth = null;
SmbFile sFile = null;
SmbFileOutputStream sfos = null;
try {
USER_NAME = "username";
PASSWORD = "password";
DOMAIN = "domain";
NETWORK_FOLDER = "smb://machineName/network_folder/";
auth = new NtlmPasswordAuthentication(
DOMAIN, USER_NAME, PASSWORD);
path = NETWORK_FOLDER + fileName;
sFile = new SmbFile(path, auth);
sfos = new SmbFileOutputStream(sFile);
sfos.write(fileContent.getBytes());
successful = true;
System.out.println("File successfully created.");
} catch (Exception e) {
successful = false;
System.err.println("Unable to create file. Cause: "
+ e.getMessage());
}
return successful;
}
}
Hope this is useful. Expecting feedback on this.
Thanks,
Marshal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With