provide-free-support Ujjwal's Blog: Copying files over network shared folder using Java

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!!

best-it-exam-    | for-our-work-    | hottst-on-sale-    | it-sale-    | tast-dumps-us-    | test-king-number-    | pass-do-it-    | just-do-it-    | pass-with-us-    | passresults-everything-    | passtutor-our-dumps-    | realtests-us-exam-    | latest-update-source-for-    | cbtnuggets-sale-exam    | experts-revised-exam    | certguide-sale-exam    | test4-sale-exam    | get-well-prepared-    | certkiller-sale-exam    | buy-discount-dumps    | how-to-get-prepared-for-the    | in-an-easy-way    | brain-dumps-sale    | with-pass-exam-guarantee    | accurate-study-material    | at-first-try    | 100%-successful-rate    | get-certification-easily    | material-provider-exam    | real-exam-practice    | with-pass-score-guarantee    | certification-material-provider    | for-certification-professionals    | get-your-certification-successfully    | 100%-Pass-Rate    | in-pdf-file    | practice-exam-for    | it-study-guides    | study-material-sku    | study-guide-pdf    | prep-guide-demo    | certification-material-id    | actual-tests-demo    | brain-demos-test    | best-pdf-download    | our-certification-material    | best-practice-test    | leading-provider-on    | this-course-is-about    | the-most-reliable    | high-pass-rate-of    | high-pass-rate-demo    | recenty-updated-key    | only-for-students-free-download    | courseware-plus-kit-for    | accurate-answers-of    | the-most-reliable-id    | provide-training-for    | welcome-to-buy    | material-for-success-pass    | provide-free-support    | best-book-for-pass    | accuracy-of-the-answers    | pass-guarantee-id    | pass-exam-key    | pass-sku-id    | study-for-exid    | pdf-sku-exid    | sku-pdf-id    | pdf-demo-key    | exam-demo-ske    | pass-it-dump    |
http://www.menuiserie-dauvergne.com/    | http://www.menuiserie-dauvergne.com/    |