Access to file using Java with Samba JCIFS
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.
- download the jar file http://jcifs.samba.org/ (there is only one jar file)
- copy and paste the below code with your information for user name, password and shared folder and that's all you need
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 !");
Accessing Files using Java with Samba JCIFS
try this one
try{
String user = "your_user_name";
String pass ="your_pass_word";
String sharedFolder="shared";
String url = "smb://ip_address/" + sharedFolder + "/test.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
null, user, pass);
SmbFile sfile = new SmbFile(url, auth);
}catch(Exception e){
}
AndroidManisfest.xml permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Simplest way to read a file using jcifs
SmbFile file = null;
byte[] buffer = new byte[1024];
try {
String url = "smb://"+serverAddress+"/"+sharename+"/TEST.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
file = new SmbFile(url, auth);
try (SmbFileInputStream in = new SmbFileInputStream(file)) {
int bytesRead = 0;
do {
bytesRead = in.read(buffer)
// here you have "bytesRead" in buffer array
}
while (bytesRead > 0);
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "ERROR: "+e);
}
or even better, assuming that you're working with text files - using BufferedReader
from Java SDK:
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(file)))) {
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
}
}
And write with:
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new SmbFileOutputStream(file)))) {
String toWrite = "xxxxx";
writer.write(toWrite, 0, toWrite.length());
}
Java JCIFS how right to copy file from Samba to Windows local?
option resolve to the problem
InputStream in = null;
OutputStream out = null;
try{
String SambaURL= "smb://usersamba:1234@192.168.1.110/data/1b.csv";
File destinationFolder = new File("C:\\Temp\\IN\\");
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS_");
File child = new File (destinationFolder+ "/" + fmt.format(new Date()) +"1b.csv");
SmbFile dir = new SmbFile(SambaURL);
SmbFile fileToGet=new SmbFile(SambaURL);
fileToGet.connect();
in = new BufferedInputStream(new SmbFileInputStream(fileToGet));
out = new BufferedOutputStream(new FileOutputStream(child));
byte[] buffer = new byte[4096];
int len = 0; //Read length
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
out.flush(); //The refresh buffer output stream
}
catch (Exception e) {
String msg = "The error occurred: " + e.getLocalizedMessage();
System.out.println(msg);
}
finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
}
catch (Exception e) {}
}
source here
Write/upload a file using Samba/JCIFS issue (SmbAuthException: Access is denied)
OMG!!! Solution was so simple!!! To access network which is not login/password protected and thus doesn't need any authorization is not NtlmPasswordAuthentication.ANONYMOUS BUT it is:
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, null, null);
damn it wasn't that obvious!
How to copy file from smb share to local drive using jcifs in Java?
maybe adding auth to the second file:
SmbFile dest = new SmbFile ("C:/SQLRESTORESTAGE/v2.bak",**auth**);
using SmbFile dest = new SmbFile ("C:/SQLRESTORESTAGE",auth).canWrite
you know if you have write permissions on the parent directory
Related Topics
Pass Data from Java Servlet to Jsp
Is There a Way in Java to Convert an Integer to Its Ordinal Name
Parsing JSON Array Within JSON Object
Displaying Fancy Equations with Java
How to Change Java Version Used by Tomcat
Annotations from Javax.Validation.Constraints Not Working
How to Configure Hibernate Config File for SQL Server
Declaring Floats, Why Default Type Double
Error: Java_Home Is Not Defined Correctly Executing Maven
Java Parsing Xml Document Gives "Content Not Allowed in Prolog." Error
Block()/Blockfirst()/Blocklast() Are Blocking Error When Calling Bodytomono After Exchange()
How to Hide a Jframe in System Tray of Taskbar
Read Error Response Body in Java
Reading a Binary Input Stream into a Single Byte Array in Java
Java.Lang.Classnotfoundexception: Org.Springframework.Boot.Springapplication Maven