Answer the question
In order to leave comments, you need to log in
Java JCIFS is it possible to copy (transfer) files from samba to windows?
Hello.
I'm trying to write an application in java that would copy (transfer) files from samba to the windows folder.
For these purposes, I found the samba JCIFS library.
There is an example of copying inside samba.
smbFromFile = new SmbFile("smb://...pool/from-here/the-file.pdf", auth);
smbToFile = new SmbFile("smb://...pool/to-here/the-file.pdf", auth);
smbFromFile.copyTo(smbToFile);
String user = "usersamba";
String pass ="1234";
String hostname = "192.168.10.1";
String sharedFolder="data/new";
String path="smb://"+hostname+"/"+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 !");
SmbFile smbFromFile = new SmbFile("smb://192.168.10.1/data", auth);
File destinationFolder = new File("C:\\Temp\\New\\");
Answer the question
In order to leave comments, you need to log in
Solution here
example with samba on windows
InputStream in = null;
OutputStream out = null;
try{
String SambaURL= "smb://usersamba:[email protected]/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) {}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question