Sometimes we need to create structure of a local directory on a remote FTP server, without uploading files to the server. That means creating a copy structure of the local directory on the server by creating only directories, not uploading files.

Based on the code presented in the article: How to upload a directory to a FTP server, by making some slightly modifications, we create a cut-down version of the utility class as follows:

import java.io.File;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;

/**
 * This utility class provides a method for uploading only structure of a
 * directory from local computer to a remote FTP server, based on Apache Commons
 * Net library.
 *
 * @author www.codejava.net
 *
 */
public class FTPUploadUtil {
	/**
	 * Upload structure of a directory (without uploading files) to a FTP
	 * server.
	 *
	 * @param ftpClient
	 *            an instance of org.apache.commons.net.ftp.FTPClient class.
	 * @param remoteDirPath
	 *            Path of the destination directory on the server.
	 * @param localParentDir
	 *            Path of the local directory being uploaded.
	 * @param remoteParentDir
	 *            Path of the parent directory of the current directory on the
	 *            server (used by recursive calls).
	 * @throws IOException
	 *             if any network or IO error occurred.
	 */
	public static void uploadDirStructure(FTPClient ftpClient,
			String remoteDirPath, String localParentDir, String remoteParentDir)
			throws IOException {

		File localDir = new File(localParentDir);
		File[] subFiles = localDir.listFiles();
		if (subFiles != null && subFiles.length > 0) {
			for (File item : subFiles) {
				if (item.isDirectory()) {
					String remoteFilePath = remoteDirPath + "/"
							+ remoteParentDir + "/" + item.getName();
					if (remoteParentDir.equals("")) {
						remoteFilePath = remoteDirPath + "/" + item.getName();
					}

					// create directory on the server
					boolean created = ftpClient.makeDirectory(remoteFilePath);
					if (created) {
						System.out.println("CREATED the directory: "
								+ remoteFilePath);
					} else {
						System.out.println("COULD NOT create the directory: "
								+ remoteFilePath);
					}

					// upload the sub directory
					String parent = remoteParentDir + "/" + item.getName();
					if (remoteParentDir.equals("")) {
						parent = item.getName();
					}

					localParentDir = item.getAbsolutePath();
					uploadDirStructure(ftpClient, remoteDirPath, localParentDir,
							parent);
				}
			}
		}
	}
}
Here is code of the test program:

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;

public class UploadDirectoryStructureTest {

	public static void main(String[] args) {
		String server = "www.codejava.net";
		int port = 21;
		String user = "username";
		String pass = "password";

		FTPClient ftpClient = new FTPClient();

		try {
			// connect and login to the server
			ftpClient.connect(server, port);
			ftpClient.login(user, pass);

			// use local passive mode to pass firewall
			ftpClient.enterLocalPassiveMode();

			System.out.println("Connected");

			String remoteDirPath = "/Upload";
			String localDirPath = "D:/Test";

			FTPUploadUtil.uploadDirStructure(ftpClient, remoteDirPath,
					localDirPath, "");

			// log out and disconnect from the server
			ftpClient.logout();
			ftpClient.disconnect();

			System.out.println("Disconnected");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
Compile the utility class and the test program:

javac -cp commons-net-3.6.jar;. UploadDirectoryStructureTest.java

Run the test program:

java -cp commons-net-3.6.jar;. UploadDirectoryStructureTest

If the local directory D:/Test has the following structure:

directory structure to upload

Then the test program will output the following:

output of program to upload directory structure



 

NOTES:Download the latest distribution of the Apache Commons Net library here.

 

Related Java FTP File Upload Tutorials:

 

Other Java File Upload Tutorials:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (FTPUploadUtil.java)FTPUploadUtil.java[Utility class]2 kB
Download this file (UploadDirectoryStructureTest.java)UploadDirectoryStructureTest.java[Test program]0.9 kB

Add comment