Java Tutorial – Using JCIFS to copy files to shared network drive using username and password

This is a quick and dirty example of how you can copy the files to shared network drive. We have tested this under Microsoft Windows, Mac and Linux platforms. To do the actual file copy, we have used an readily available and highly popular API called JCIFS. JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java. CIFS is the standard file sharing protocol on the Microsoft Windows platform. Visit their website at: jcifs.samba.org if you want to download this API.

As you can see in the sample program below, it allows you to even connect to protected network shares using userid and password.

package com.kushal.tools;

/**
 * @author Kushal Paudyal
 * Create on 2012-10-12
 * Last Modified On 2012-10-12
 * www.sanjaal.com/java, www.icodejava.com
 *
 * JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java.
 * CIFS is the standard file sharing protocol on the Microsoft Windows platform
 * Visit their website at: jcifs.samba.org
 *
 * Uses: jcifs-1.1.11.jar
 *
 */
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

public class NetworkShareFileCopy {
	static final String USER_NAME = "admin";
    static final String PASSWORD = "password";
    //e.g. Assuming your network folder is: \my.myserver.netsharedpublicphotos
    static final String NETWORK_FOLDER = "smb://my.myserver.net/shared/public/photos";

	public static void main(String args[]) {
		String fileContent = "This is a test file";

		new NetworkShareFileCopy().copyFiles(fileContent, "testFile.text");
	}

	public boolean copyFiles(String fileContent, String fileName) {
		boolean successful = false;
		 try{
	            String user = USER_NAME + ":" + PASSWORD;
	            System.out.println("User: " + user);

	            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
	            String path = NETWORK_FOLDER + fileName;
	            System.out.println("Path: " +path);

	            SmbFile sFile = new SmbFile(path, auth);
	            SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
	            sfos.write(fileContent.getBytes());

	            successful = true;
	            System.out.println("Successful" + successful);
	        } catch (Exception e) {
	            successful = false;
	        	e.printStackTrace();
	        }
		return successful;
	}
}

  1. How to extract plain Text from HTML Website easily in Java
  2. How to read / write UTF8 and Non-UTF8 files in Java
  3. How To Print A Text File In Java
  4. How to read a tab separated or tab delimited file in Java program and print the content to console
  5. How To Find What Java Version You Are Using?
  6. Computing the total, free and usable disk space easily using JDK 1.6
  7. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  8. Creating java.util.date and java.util.sql objects from Calendar object
  9. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  10. Bar Code Generation In Java Using iText API
  11. Simple Tag Generator In Java For Music Blogs
  12. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  13. Calling URL Browser From Java Application
  14. Java Reflection Tutorial – List Methods Of A Class
  15. Cleaning Punctuation And White Spaces From A String
  16. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  17. Prime Number Finder In Java
  18. Utility Class For Getting SQL Connection and Closing It
  19. Java Design Pattern – Singleton Pattern
  20. Reading / Writing File in Java and String Manipulation
  21. How to capture a screenshot using Java and save it to a file?
  22. Calculating The Difference Between Two Dates In Terms of Months
  23. Getting The Name Of Currently Logged In User In The System
  24. How To Set And Get System Clipboard Contents In Java
  25. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  26. Generating Nepali Unicode Sequence For Characters
  27. How To Read DOC file Using Java and Apache POI
  28. Writing To Excel File Using Apache POI
  29. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  30. Replacing non-ASCII characters using Java Regular Expressions
  31. Reading Excel File Using Java And Apache POI
  32. Calculating Folder Size In Java
  33. How To Rename A File In Java
  34. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

One Response to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

Leave a Reply

Your email address will not be published. Required fields are marked *