Home > Apache Mina > How to start a simple embedded java based FTP server

How to start a simple embedded java based FTP server

I recently needed to run a simple FTP server for a test, and found a nice solution: Apache FTP Server. This is a side-project of Apache MINA.

Look at the bottom of the download page to find the maven dependency coordinates (GAV).

Once you have ftpserver-core on the classpath you can start a simple FTP server on a non-root port, with user authentication using this code:


import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;

public class FTPTestServer {
	
	public static void main(String[] args) throws FtpException {
		PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
		UserManager userManager = userManagerFactory.createUserManager();
		BaseUser user = new BaseUser();
		user.setName("username");
		user.setPassword("password");
		user.setHomeDirectory("/tmp");
		userManager.save(user);
		
		ListenerFactory listenerFactory = new ListenerFactory();
		listenerFactory.setPort(2221);
		
		FtpServerFactory factory = new FtpServerFactory();
		factory.setUserManager(userManager);
		factory.addListener("default", listenerFactory.createListener());
		
		FtpServer server = factory.createServer();
		server.start();
	}
}


Advertisements
Categories: Apache Mina Tags: , ,
  1. Hemant
    June 19, 2017 at 8:56 pm

    Very helpful, thank you.

  1. January 16, 2016 at 5:29 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: