Search code examples
javascalaftpapache-mina

How to start Apache MINA FTP server using Scala/Java?


I'm trying to embed an Apache MINA FTP server in my Scala application, and am having trouble spiking it out. I'm using Apache FtpServer 1.05, and have found a couple of examples on their site which don't seem to work when I Scala-ize them.

Here's my code:

package aperture

import org.apache.ftpserver.listener.ListenerFactory
import org.apache.ftpserver.ftplet._
import org.apache.ftpserver.{FtpServerFactory, FtpServer}
import java.io.File
import org.apache.ftpserver.usermanager.{UserFactory, SaltedPasswordEncryptor, PropertiesUserManagerFactory}

object Main {
    def main(args: Array[String]) {
        val serverFactory: FtpServerFactory = new FtpServerFactory()
        val listenerFactory: ListenerFactory = new ListenerFactory()

        listenerFactory.setPort(2221);
        listenerFactory.setServerAddress("localhost")
        listenerFactory.setImplicitSsl(false);

        serverFactory.addListener("default", listenerFactory.createListener())

        val userManagerFactory: PropertiesUserManagerFactory = new PropertiesUserManagerFactory()
        userManagerFactory.setFile(new File("myusers.properties"))
        userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor())
        val userManager: UserManager = userManagerFactory.createUserManager()

        val userFact: UserFactory = new UserFactory()
        userFact.setName("myNewUser")
        userFact.setPassword("secret")
        userFact.setHomeDirectory("ftproot")
        val user: User = userFact.createUser()
        userManager.save(user)

        serverFactory.setUserManager(userManagerFactory.createUserManager())

        // start the server
        val server: FtpServer = serverFactory.createServer()
        server.start()
    }
}

The code's valid, and the server appears to start on port 2221, but I can't connect to it: ftp: localhost:2221: No address associated with hostname and ftp: 127.0.0.1:2221: Name or service not known.

Any thoughts?


Solution

  • I was doing two things wrong:

    1. I was connecting to the ftp server using the command ftp localhost:2221, instead of the correct way (with a space instead of a colon) ftp localhost 2221.
    2. I created a UserManager, but never ended up using it. I changed serverFactory.setUserManager(userManagerFactory.createUserManager()) to serverFactory.setUserManager(userManager).