Copying files very slowly to a Windows network using JCIF

I am trying to copy a file from my local computer to a shared folder on a Windows server. This is the function 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 did not work for me. When I do a normal copy (copy and paste), a file with a size of 25 MB requires a maximum of 8 minutes . But when I use my java program using this function, it takes more than 20 minutes . How to make this copy faster? Thanks in advance.

+4
source share
4 answers

In case it helps others ... I had a similar problem, but in a different direction (slow copying in Windows using JCIFS). The problem was resolved by adding

  -Djcifs.resolveOrder = DNS

to the property list. (The default inclusion of BCAST - to send a NetBIOS name request passed to 255.255.255.255 - is what caused a huge delay.)

+2
source

try this feature, highly optimized if it is still slow, and then increase the size of the buffer in the code. In my case, it reduced the time from 10 minutes to copy a 48 MB file for 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; } 
+1
source

I noticed that jCIFS does "something" (afair jcifs.smb.SmbTransport.checkStatus (..)) for each fragment that it reads, i.e. for each fragment that is read into the buffer. This means that increasing the size of your buffer can really speed up the process, although the real problem still exists, it only happens 1 or 2 times, which has less impact on the total time.

This helps a lot to set "jcifs.util.loglevel = 3" and see what is really wrong.

In my case, I had to set "jcifs.smb.client.dfs.disabled = false" at the end, since "jcifs.resolveOrder = DNS" did not help ..

+1
source

I had the same problem. Tried -Djcifs.resolveOrder = DNS with no luck. After reading a few comments with the size of the buffer, I decided to go to extremes and really expand it. I know that my transfer rate should be at least 50 Mb / s, so I converted it to bytes and set it as my buffer, and it worked as expected.

0
source

Source: https://habr.com/ru/post/1495884/


All Articles