Tuesday, August 6, 2013

Copying files over network shared folder using Java

Problem :: One of my blog reader posted a problem that he cannot copy files over a network shared folder using java.

Solution :: Java alone doesn't have support for networked file sharing. But if you use a library, like Samba, then you can. 

You can check below source code which successfully copies file from my local drive to a shared network folder ::

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networkupload;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import jcifs.Config;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

/**
 *
 * @author ujjwal
 */
public class NetworkUpload {

     public static void main(String[] args) throws IOException {
  final String userName = "Ujjwal";
  final String password = "ujjwal12345";
  final String domain = "ujjwal12345";
  //source path begins with / as i am using macbook (mac osx), in your case it will be windows drive path
  final String sourcePath = "/Developer/test.pdf";
  final String destinationPath = "smb://192.168.60.14/ftp/test.pdf";

  copyFileUsingJcifs(domain,userName, password, sourcePath, destinationPath);

  System.out.println("The file has been copied using JCIFS");
 }

 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[16 * 1024 * 1024];
  int len;
  while ((len = fileInputStream.read(buf)) > 0) {
   smbFileOutputStream.write(buf, 0, len);
  }
  fileInputStream.close();
  smbFileOutputStream.close();
 }
  
}

If you face any issues implementing this code, you can contact me ::
Cheers,
Ujjwal Soni

9 comments:

Unknown said...

Thank you very much sir,This is very very good example.

Sai Pradeep Dandem said...

Hi, Thanks for the blog.
However can you help me about "how to create a shared folder" using java. May be by giving all rights to that folder.
Thanks,
Sai.

Unknown said...

Hi,

I was trying to copy a local file to smb folder and file.copyto(dest); was not working for me.

Thanks for the solution. I wish I found this result first. Thanks a lot. This helped me.

Unknown said...

Thanks Sir Its a useful article for me.

Unknown said...

Hi, Thanks for the blog. I have a query regarding zip file creation using SmbFileOutputStream.
I have tried to create zip file from my local linux machine to remote windows server. The files are created but couldn't open the zip file.

Anonymous said...

Hi, i am not getting connected through this code. Getting jcifs.smb.SmbException: Failed to connect to server. Please help mi

Unknown said...

Why to use so much of coding when a software can do that for you. Automate the task and sit back to relax. I am using GS Richcopy 360 enterprise edition which does all the work for you with multi threaded file transfer, saving a lot of time. Try it, hope it helps someone!

Unknown said...

Tons of Thanks mate for this code.. Please keep posting :D

Afro said...

Wah! Thank you!!