Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy file from SMB share to local drive not in domain with JCIFS

Tags:

smb

jcifs

I'm trying to copy some remote files to the local drive, in Java, using JCIFS. The remote machine is inside a domain. The local machine is not in a domain.

The following code works, but it's really slow (2 minutes for 700Kb... and I have many Mb...):

SmbFile remoteFile = new SmbFile("smb://...")
OutputStream os = new FileOutputStream("/path/to/local/file");
InputStream is = remoteFile.getInputStream();
int ch;
while ((ch = is.read()) != -1) {
    os.write(ch);
}
os.close();
is.close();

I think I could use SmbFile.copyTo(), but I don't know how to access the local file. If I write the following, I get a connection error:

localfile = new SmbFile("file:///path/to/localfile")

This question is related to How to copy file from smb share to local drive using jcifs in Java?

like image 244
user1922691 Avatar asked Dec 12 '22 18:12

user1922691


1 Answers

You just need to make bigger buffer:

SmbFile remoteFile = new SmbFile("smb://...")
try(OutputStream os = new FileOutputStream("/path/to/local/file")){
try(InputStream is = remoteFile.getInputStream())
{
    int bufferSize = 5096;

    byte[] b = new byte[bufferSize];
    int noOfBytes = 0;
    while ((noOfBytes = is.read(b)) != -1) {
        os.write(b, 0, noOfBytes);
    }
}}

Here some test I've done with file 23 Mb, using mentioned code.

bufferSize = 1024 Elapsed time : 10.9587606066 sec

bufferSize = 4096 Elapsed time : 5.6239662951 sec

bufferSize = 5096 Elapsed time : 5.0798761245 sec

bufferSize = 5096 Elapsed time : 4.879439883 sec

bufferSize = 10240 Elapsed time : 4.0192989201 sec

bufferSize = 50240 Elapsed time : 3.8876541543 sec

bufferSize = 100240 Elapsed time : 3.742167582 sec

like image 80
maxmimko Avatar answered Jan 14 '23 17:01

maxmimko