【问题标题】:Writing a Java FTP server编写 Java FTP 服务器
【发布时间】:2012-02-16 15:40:03
【问题描述】:

我正在尝试编写一个代码,在我的独立设备上打开一个 FTP 服务器,这样我就可以将文件从它复制到另一台计算机上的客户端,反之亦然,但我对服务器端编程非常陌生,不会了解如何。

我得到了Apache FtpServer,但对它的使用有点困惑,我正在寻找如何使用它的基本步骤。可能是这样的:

  1. 执行连接命令
  2. 登录
  3. 做一些事情....

【问题讨论】:

    标签: java ftp ftp-server


    【解决方案1】:

    让我为你写一个基本的例子,使用非常有用的Apache FtpServer

    FtpServerFactory serverFactory = new FtpServerFactory();
    ListenerFactory factory = new ListenerFactory();
    factory.setPort(1234);// set the port of the listener (choose your desired port, not 1234)
    serverFactory.addListener("default", factory.createListener());
    PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
    userManagerFactory.setFile(new File("/home/blablah/myusers.properties"));//choose any. We're telling the FTP-server where to read its user list
    userManagerFactory.setPasswordEncryptor(new PasswordEncryptor()
    {//We store clear-text passwords in this example
    
            @Override
            public String encrypt(String password) {
                return password;
            }
    
            @Override
            public boolean matches(String passwordToCheck, String storedPassword) {
                return passwordToCheck.equals(storedPassword);
            }
        });
        //Let's add a user, since our myusers.properties file is empty on our first test run
        BaseUser user = new BaseUser();
        user.setName("test");
        user.setPassword("test");
        user.setHomeDirectory("/home/blablah");
        List<Authority> authorities = new ArrayList<Authority>();
        authorities.add(new WritePermission());
        user.setAuthorities(authorities);
        UserManager um = userManagerFactory.createUserManager();
        try
        {
            um.save(user);//Save the user to the user list on the filesystem
        }
        catch (FtpException e1)
        {
            //Deal with exception as you need
        }
        serverFactory.setUserManager(um);
        Map<String, Ftplet> m = new HashMap<String, Ftplet>();
        m.put("miaFtplet", new Ftplet()
        {
    
            @Override
            public void init(FtpletContext ftpletContext) throws FtpException {
                //System.out.println("init");
                //System.out.println("Thread #" + Thread.currentThread().getId());
            }
    
            @Override
            public void destroy() {
                //System.out.println("destroy");
                //System.out.println("Thread #" + Thread.currentThread().getId());
            }
    
            @Override
            public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException
            {
                //System.out.println("beforeCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine());
                //System.out.println("Thread #" + Thread.currentThread().getId());
    
                //do something
                return FtpletResult.DEFAULT;//...or return accordingly
            }
    
            @Override
            public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException
            {
                //System.out.println("afterCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine() + " | " + reply.getMessage() + " : " + reply.toString());
                //System.out.println("Thread #" + Thread.currentThread().getId());
    
                //do something
                return FtpletResult.DEFAULT;//...or return accordingly
            }
    
            @Override
            public FtpletResult onConnect(FtpSession session) throws FtpException, IOException
            {
                //System.out.println("onConnect " + session.getUserArgument() + " : " + session.toString());
                //System.out.println("Thread #" + Thread.currentThread().getId());
    
                //do something
                return FtpletResult.DEFAULT;//...or return accordingly
            }
    
            @Override
            public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException
            {
                //System.out.println("onDisconnect " + session.getUserArgument() + " : " + session.toString());
                //System.out.println("Thread #" + Thread.currentThread().getId());
    
                //do something
                return FtpletResult.DEFAULT;//...or return accordingly
            }
        });
        serverFactory.setFtplets(m);
        //Map<String, Ftplet> mappa = serverFactory.getFtplets();
        //System.out.println(mappa.size());
        //System.out.println("Thread #" + Thread.currentThread().getId());
        //System.out.println(mappa.toString());
        FtpServer server = serverFactory.createServer();
        try
        {
            server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set
        }
        catch (FtpException ex)
        {
            //Deal with exception as you need
        }
    

    请注意,在服务器端,您不必手动处理连接、登录等:Ftplet 会为您完成。

    但是,您可以在匿名内部 Ftplet 类的重写方法中添加自定义的预处理[或后]处理(当您使用 new Ftplet(){ ... } 实例化它时。

    【讨论】:

    • FtpServerFactory 来自哪里?
    • 它来自 Apache FtpServer 核心(OP 在他的问题中说他得到了 Apache FtpServer API 但不知道从哪里开始)。无论如何,我忘记明确我在回答中使用的是 Apache FtpServer;我马上去。感谢您的评论。
    • 我要编写服务器方法并稍微玩一下...如果我遇到问题,我可以在几天后在这里为你发帖吗?
    • 根据 StackOverflow 的实践,您应该发布一个单独的问题,非常针对您发现的新问题。不过,我会密切关注您帐户的问题,如果我能够回答您提出的问题,我一定会的!
    • 您好我有一个严重的问题,当 FTP Mina 正在运行时,如果我关闭了连接。 Ftp Mina 没有停止。我花了差不多一个半月的时间。任何想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多