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