Question

I can copy file from share to local. But I want to switch, and copy a file from local to share.

I am trying this code:
SmbFile oldFile = new SmbFile("c:/tmp/test.xml");
SmbFile newFile = new SmbFile("smb://someone_pc/tmp/test.xml", new NtlmPasswordAuthentication("", username, password));
oldFile.copyTo(newFile);

But I am getting an exception on copyTo method:

Invalid operation for workgroups or servers

How should I copy file from local to share?

No correct solution

OTHER TIPS

Complete solution for copying file from local to shared disk over SMB protocol using streams like Javi_Swift (writing in parts - solution for large files where it's not possible to load whole file into memory):

// local source file and target smb file
File fileSource = new File("C:/example.jpg");
SmbFile smbFileTarget = new SmbFile(smbFileContext, "example.jpg");
// input and output stream
FileInputStream fis = new FileInputStream(fileSource);
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFileTarget);
// writing data
try {
    // 16 kb
    final byte[] b  = new byte[16*1024];
    int read = 0;
    while ((read=fis.read(b, 0, b.length)) > 0) {
        smbfos.write(b, 0, read);
    }
}
finally {
    fis.close();
    smbfos.close();
}

It was some time ago I worked with jcifs. Could you try newFile.createNewFile(); and then use the copyTo. If that doesn't work, than try newFile.getOutputStream() and write the data to this stream instead of using copyTo.

I´m a bit late to the party but this could be useful for other persons coming to this question.

I upload files from local to share using streams and it´s working without any problem. My code is:

SmbFile remoteFile =  new SmbFile(remotePath, auth);
SmbFileOutputStream out = new SmbFileOutputStream(remoteFile);
FileInputStream fis = new FileInputStream(localFile);
out.write(IOUtils.toByteArray(fis));
out.close();

An example using Java 1.7's Files class:

Path source = Paths.get("c:/tmp/test.xml");
SmbFile newFile = new SmbFile("smb://someone_pc/tmp/test.xml", new NtlmPasswordAuthentication("", username, password));
try (OutputStream out = newFile.getOutputStream())
{
    Files.copy(source, out);
}
public void uploadToSmb(String destinationPath,File localFile){
    public final static byte[] BUFFER = new byte[10 * 8024];
        ByteArrayInputStream inputStream = null;
        SmbFileOutputStream sfos = null;
        try {
            String user = username + ":" + password;
            int lenghtOfFile = (int) localFile.length();
            byte[] data = FileUtils.readFileToByteArray(localFile);
            inputStream = new ByteArrayInputStream(data);
            String path = destinationPath + localFile.getName();
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
            remoteFile = new SmbFile(path, auth);
            sfos = new SmbFileOutputStream(remoteFile);
            long total = 0;
            while ((count = inputStream.read(BUFFER)) > 0) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                int percentage = (int) ((total / (float) lenghtOfFile) * 100);
                publishProgress(percentage);
                //  publishProgress((int) ((total * 100) / lenghtOfFile));
                // writing data to file
                    sfos.write(BUFFER,0,count);

            }
                sfos.flush();
                inputStream.close();
                sfos.close();

        } catch (Exception e) {
            e.printStackTrace();
        } 
      }  

this code is working Great...

Since the last answer the NtlmPasswordAuthentication has been deprecated. Therefore, this is how I solved, for example, copying a file to a samba share:

String user="your_samba_username";
String pass="your_samba_password";
String path="smb://samba_ip_address/outputdir/outputfile.txt";
try {
    InputStream  winfis = new FileInputStream(new File("c:/inputdir/inputfile.txt"));
    try {
        SingletonContext baseContext = SingletonContext.getInstance(); 
        Credentials credentials=new NtlmPasswordAuthenticator(null,user,pass); 
        CIFSContext testCtx = baseContext.withCredentials(credentials);
        try {             
            SmbFile smbFile = new SmbFile(path, testCtx);
            SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
            try {
                IOUtils.copy(winfis, smbfos);
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {    
                try {
                    smbfos.close();
                } catch (IOException ex) {
                     ex.printStackTrace();
                }
            }
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    } catch (CIFSException ex) {
        ex.printStackTrace();
    } finally {
        try {
            winfis.close();
        } catch (IOException ex) {
             ex.printStackTrace();
        }
    }
} catch (FileNotFoundException ex) {
    ex.printStackTrace();
}

Required libraries (dependencies):

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top