N
N
Nikolay Baranenko2016-07-21 12:05:26
Java
Nikolay Baranenko, 2016-07-21 12:05:26

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);


An example of creating a file on samba
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 !");


Checked both work.

BUT, so far it has not been possible to find a solution to copy files from samba to the local windows folder, provided that the application will be launched from the windows server.

Please help me to solve the problem.

How to make friends
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

1 answer(s)
N
Nikolay Baranenko, 2016-07-21
@drno-reg

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 question

Ask a Question

731 491 924 answers to any question