I'm trying to copy a file from my local machine to Shared folder in a windows server. This is the function which I used.
public static void copyFileUsingJcifs(final String domain, final String userName, final String password, final String sourcePath, final String destinationPath) throws IOException {
final NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domain, userName, password);
final SmbFile sFile = new SmbFile(destinationPath, auth);
final SmbFileOutputStream smbFileOutputStream = new SmbFileOutputStream(sFile);
final FileInputStream fileInputStream = new FileInputStream(new File(
sourcePath));
final byte[] buf = new byte[16384];
int len;
while ((len = fileInputStream.read(buf)) > 0) {
smbFileOutputStream.write(buf, 0, len);
}
fileInputStream.close();
smbFileOutputStream.close();
}
I tried this answer, but didn't work for me. When I do normal copying(Copy and Paste) it only takes maximum of 8minutes for a 25MB file. But when I use my java program using this function its taking more than 20minutes. How can I make this copying faster? Thanks in advance.
In case it helps others...I had a similar issue, but in the other direction (slow copying TO Windows using JCIFS). The issue was resolved by adding
-Djcifs.resolveOrder=DNS
to the property list. (The default inclusion of BCAST -- to send a NetBIOS name query broadcast to 255.255.255.255 -- is what was causing the enormous delay.)
try this function its highly optimized if its still slow then increase buffer size in code . In my case it reduced time from 10 min to copy 48MB file to 1 min
public static boolean createCopyOnNetwork(String domain,String username,String password,String src, String dest) throws Exception
{
//FileInputStream in = null;
SmbFileOutputStream out = null;
BufferedInputStream inBuf = null;
try{
//jcifs.Config.setProperty("jcifs.smb.client.disablePlainTextPasswords","true");
NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication(domain,username,password); // replace with actual values
SmbFile file = new SmbFile(dest, authentication); // note the different format
//in = new FileInputStream(src);
inBuf = new BufferedInputStream(new FileInputStream(src));
out = (SmbFileOutputStream)file.getOutputStream();
byte[] buf = new byte[5242880];
int len;
while ((len = inBuf.read(buf)) > 0){
out.write(buf, 0, len);
}
}
catch(Exception ex)
{
throw ex;
}
finally{
try{
if(inBuf!=null)
inBuf.close();
if(out!=null)
out.close();
}
catch(Exception ex)
{}
}
System.out.print("\n File copied to destination");
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With