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://";

  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(
  final FileInputStream fileInputStream = new FileInputStream(new File(

  final byte[] buf = new byte[16 * 1024 * 1024];
  int len;
  while ((len = fileInputStream.read(buf)) > 0) {
   smbFileOutputStream.write(buf, 0, len);

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


Mahesh Mali 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.

pranav kumar said...


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.

Rasel zaman2 said...

Thanks Sir Its a useful article for me.

sijin jose 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.