Java FTP Upload only structure of a directory to server
- Details
- Written by Nam Ha Minh
- Last Updated on 20 July 2019   |   Print Email
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:Then the test program will output the following:Related Java FTP File Upload Tutorials:
- Upload files to FTP server using URLConnection class
- How to upload a directory to a FTP server
- Swing application to upload files to FTP server with progress bar
Other Java File Upload Tutorials:
- Java Servlet File Upload Example
- Spring MVC File Upload Tutorial
- Struts File Upload Example
- Java Upload files to database (Servlet + JSP + MySQL)
Other Java FTP Tutorials:
- Connect and login to a FTP server
- Java FTP create directory example
- Java FTP example - Change working directory
- Java FTP list files and directories example
- Java FTP file download tutorial and example
- Java FTP delete file example
- Java FTP example - Search for files and directories
Comments