Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance: Very slow file copying to Windows network using JCIF

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.

like image 969
Nirmal Raghavan Avatar asked Feb 15 '23 15:02

Nirmal Raghavan


2 Answers

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.)

like image 92
Glenn Avatar answered May 01 '23 07:05

Glenn


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;
}
like image 28
fahad Avatar answered May 01 '23 05:05

fahad