Monday, 19 August 2013

copy folder using smb , copy folder from smb share to local drive using jcifs in Java

We are already discuss about JCIFS SMB client library to know check it older post now here discuss about jcifs copyTo method :-


public void copyTo(SmbFile dest)throws SmbException

This method will copy the file or directory represented by this SmbFile and it's sub-contents to the location specified by the dest parameter. This file and the destination file do not need to be on the same host. This operation does not copy extended file attibutes such as ACLs but it does copy regular attributes as well as create and last write times. This method is almost twice as efficient as manually copying as it employs an additional write thread to read and write data concurrently.

Sample Code :-
package com.javastoreroom.myapp;

import java.net.MalformedURLException;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

import org.apache.log4j.Logger;

public class MoveFolder {
 
 private static final Logger LOGGER = Logger.getLogger(MoveFolder.class);
 public static void main(String[] args) {
  
  NtlmPasswordAuthentication  ntlmPasswordAuthentication = new NtlmPasswordAuthentication(null, "Arun", "********"); 

  try {
   SmbFile source = new SmbFile("smb://192.168.1.170/C_Volume/EmployeeInfo/" ,ntlmPasswordAuthentication);
   LOGGER.info("************* Source Location Authenticate *****************");

   SmbFile destination = new SmbFile("smb://192.168.1.170/D_Volume/AllBackUp" , ntlmPasswordAuthentication);
   LOGGER.info("************* Destination Location  Authenticate ************");

   //--------- final process to move folder
   source.copyTo(destination);
            LOGGER.info("**** Content have been Move from one directory to another ****");
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (SmbException e) {
   e.printStackTrace();
  }

 }

}

After executing application all file, folder are copy to destination directory.


No comments:

Post a Comment