3

我正在尝试将 Apache FTP 服务器集成到我的应用程序中。我已按照此处给出的说明进行操作,但遇到了一些问题。目前我可以运行服务器并从我的浏览器连接到它,但无法登录。我尝试过 admin/admin 和匿名/*,但每次登录都失败。在我下载的 apache-ftpserver-1.0.6 源代码中,与用户管理器关联的文件位于 res/conf 中,尽管当我尝试在自己的程序中匹配该文件路径时,我收到一条错误消息“无效资源目录名称”并且无法构建。我还尝试直接在 main 中包含文件 users.properties 和 ftpd-typical.xml 并且可以运行,但再次无法登录。似乎我的项目没有意识到这些文件存在。

有没有人有 Apache FTP 服务器的经验,可以告诉我包含这些文件的正确方法,以便我可以登录到我的服务器?

谢谢!

PS我不认为它应该有任何区别,但我正在为Android开发这个程序。

4

1 回答 1

4

在下面的代码中,我创建了管理员用户和非管理员用户,设置了读取、写入和限制节流和上传速率限制的限制,并施加了下载速率限制。

添加了一个侦听器来侦听用户登录和注销下载开始和下载完成事件。

 import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;

    import org.apache.ftpserver.FtpServer;
    import org.apache.ftpserver.FtpServerFactory;
    import org.apache.ftpserver.ftplet.Authority;
    import org.apache.ftpserver.ftplet.FileSystemFactory;
    import org.apache.ftpserver.ftplet.FtpException;
    import org.apache.ftpserver.ftplet.Ftplet;
    import org.apache.ftpserver.ftplet.UserManager;
    import org.apache.ftpserver.ftpletcontainer.impl.DefaultFtpletContainer;
    import org.apache.ftpserver.listener.ListenerFactory;
    import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
    import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
    import org.apache.ftpserver.usermanager.impl.BaseUser;
    import org.apache.ftpserver.usermanager.impl.ConcurrentLoginPermission;
    import org.apache.ftpserver.usermanager.impl.TransferRatePermission;
    import org.apache.ftpserver.usermanager.impl.WritePermission;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    public class SFTPServer {

        // ===========================================================
        // Constants
        // ===========================================================
        private final int FTP_PORT = 2221;
        private final String DEFAULT_LISTENER = "default";
        // private final Logger LOG = LoggerFactory.getLogger(SFTPServer.class);
        private static final List<Authority> ADMIN_AUTHORITIES;
        private static final int BYTES_PER_KB = 1024;
        private static final String DEFAULT_USER_DIR = "C:\\upload";

        public final static int MAX_CONCURRENT_LOGINS = 1;
        public final static int MAX_CONCURRENT_LOGINS_PER_IP = 1;

        // ===========================================================
        // Fields
        // ===========================================================
        private static FtpServer mFTPServer;
        private static UserManager mUserManager;
        private static FtpServerFactory mFTPServerFactory;
        private ListenerFactory mListenerFactor;

        // ===========================================================
        // Constructors
        // ===========================================================
        static {
            // Admin Authorities
            ADMIN_AUTHORITIES = new ArrayList<Authority>();
            ADMIN_AUTHORITIES.add(new WritePermission());
            ADMIN_AUTHORITIES.add(new ConcurrentLoginPermission(MAX_CONCURRENT_LOGINS, MAX_CONCURRENT_LOGINS_PER_IP));
            ADMIN_AUTHORITIES.add(new TransferRatePermission(Integer.MAX_VALUE, Integer.MAX_VALUE));
        }

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================

        // ===========================================================
        // Methods
        // ===========================================================
        public void init() throws FtpException {
            mFTPServerFactory = new FtpServerFactory();
            mListenerFactor = new ListenerFactory();
            mListenerFactor.setPort(FTP_PORT);

            mFTPServerFactory.addListener(DEFAULT_LISTENER, mListenerFactor.createListener());
            mFTPServerFactory.getFtplets().put(FTPLetImpl.class.getName(), new FTPLetImpl());

            PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
            userManagerFactory.setFile(new File("ftpusers.properties"));
            userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
            mUserManager = userManagerFactory.createUserManager();
            mFTPServerFactory.setUserManager(mUserManager);

            this.createAdminUser();
            SFTPServer.addUser("admin1", "admin1", 20, 20);

            mFTPServer = mFTPServerFactory.createServer();

            mFTPServer.start();
        }

        private UserManager createAdminUser() throws FtpException {
            UserManager userManager = mFTPServerFactory.getUserManager();
            String adminName = userManager.getAdminName();

            if (!userManager.doesExist(adminName)) {
                // LOG.info((new
                // StringBuilder()).append("Creating user : ").append(adminName).toString());
                BaseUser adminUser = new BaseUser();
                adminUser.setName(adminName);
                adminUser.setPassword(adminName);
                adminUser.setEnabled(true);
                adminUser.setAuthorities(ADMIN_AUTHORITIES);
                adminUser.setHomeDirectory(DEFAULT_USER_DIR);
                adminUser.setMaxIdleTime(0);
                userManager.save(adminUser);
            }

            return userManager;
        }

        public static void addUser(String username, String password, int uploadRateKB, int downloadRateKB) throws FtpException {

            BaseUser user = new BaseUser();
            user.setName(username);
            user.setPassword(password);
            user.setHomeDirectory(DEFAULT_USER_DIR);
            user.setEnabled(true);

            List<Authority> list = new ArrayList<Authority>();
            list.add(new TransferRatePermission(downloadRateKB * BYTES_PER_KB, uploadRateKB * BYTES_PER_KB)); // 20KB
            list.add(new ConcurrentLoginPermission(MAX_CONCURRENT_LOGINS, MAX_CONCURRENT_LOGINS_PER_IP));

            user.setAuthorities(list);

            mFTPServerFactory.getUserManager().save(user);
        }

        public static void restartFTP() throws FtpException {
            if (mFTPServer != null) {
                mFTPServer.stop();
                try {
                    Thread.sleep(1000 * 3);
                } catch (InterruptedException e) {
                }
                mFTPServer.start();
            }
        }

        public static void stopFTP() throws FtpException {
            if (mFTPServer != null) {
                mFTPServer.stop();
            }
        }

        public static void pauseFTP() throws FtpException {
            if (mFTPServer != null) {
                mFTPServer.suspend();
            }
        }

        public static void resumeFTP() throws FtpException {
            if (mFTPServer != null) {
                mFTPServer.resume();
            }
        }

        public static void main(String... are) {
            try {
                new SFTPServer().init();
            } catch (FtpException e) {
                e.printStackTrace();
            }
        }
        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================

    }

FTPLET 监听器

import java.io.IOException;

import org.apache.ftpserver.ftplet.DefaultFtplet;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.FtpletResult;

public class FTPLetImpl extends DefaultFtplet {

    @Override
    public FtpletResult onLogin(FtpSession session, FtpRequest request) throws FtpException, IOException {
        System.out.println(session.getUser().getName() + " Logged in");
        return super.onLogin(session, request);
    }

    @Override
    public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException {
        System.out.println(session.getUser().getName() + " Disconnected");
        return super.onDisconnect(session);
    }

    @Override
    public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
        System.out.println(session.getUser().getName() + " Started Downloading File " + request.getArgument());
        return super.onDownloadStart(session, request);
    }

    @Override
    public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
        System.out.println("Finished Downloading " + request.getArgument());
        return super.onDownloadEnd(session, request);
    }

}

连接到 FTP

于 2013-10-15T05:49:31.673 回答