简体   繁体   中英

How to copy file from local to share with JCifs?

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?

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´ma 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM