简体   繁体   English

如何使用 Scala/Java 启动 Apache MINA FTP 服务器?

[英]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.我正在尝试在我的 Scala 应用程序中嵌入一个 Apache MINA FTP 服务器,并且无法将其插入。 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.我正在使用Apache FtpServer 1.05 ,并在他们的网站上找到了几个示例,当我对它们进行 Scala 化时,它们似乎不起作用。

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 .代码有效,服务器似乎在端口 2221 上启动,但我无法连接到它: ftp: localhost:2221: No address associated with hostname and ftp: 127.0.0.1:2221: Name or service not known

Any thoughts?有什么想法吗?

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 .我使用命令ftp localhost:2221连接到 ftp 服务器,而不是正确的方式(使用空格而不是冒号) ftp localhost 2221
  2. I created a UserManager, but never ended up using it.我创建了一个 UserManager,但从未使用过它。 I changed serverFactory.setUserManager(userManagerFactory.createUserManager()) to serverFactory.setUserManager(userManager) .我将serverFactory.setUserManager(userManagerFactory.createUserManager())更改为serverFactory.setUserManager(userManager)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM