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?
An SmbFile object can't be constructed except with a valid smb URL. See the Constructor Summary at http://jcifs.samba.org/src/docs/api/, along with the discussion about SmbFile URLs at the top.
SmbFile URLs have the following syntax: smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]file]]][?[param=value[param2=value2[...]]]
So, if you really want to avoid using the input stream and use copyTo(), you'll have to have an SMB share on your local machine that you can point jCIFS to.
If your local machine is a Windows machine, there are some default shares that you might be able to access, like C$.
So, you could do something like:
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password") //or whatever authentication works on your local machine.
SmbFile myFile = new SmbFile("smb://localhost/C\$/path/to/localfile", auth)
Then you could use remoteFile.copyTo(myFile)
.
If you're not on a Windows host, you'll have to install Samba and setup a Samba share to connect to on your local machine... again, if you're absolutely bent on avoiding using inputStreams.