Archive

Archive for February, 2015

How to start a simple embedded java based FTP server

February 11, 2015 2 comments

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();
	}
}


Categories: Apache Mina Tags: , ,