Search in sources :

Example 21 with FtpServerFactory

use of org.apache.ftpserver.FtpServerFactory in project AnExplorer by 1hakr.

the class ConnectionsService method launchServer.

@Override
public boolean launchServer() {
    ListenerFactory listenerFactory = new ListenerFactory();
    listenerFactory.setPort(ConnectionUtils.getAvailablePortForFTP());
    FtpServerFactory serverFactory = new FtpServerFactory();
    serverFactory.addListener("default", listenerFactory.createListener());
    ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();
    connectionConfigFactory.setAnonymousLoginEnabled(getNetworkConnection().isAnonymousLogin());
    connectionConfigFactory.setMaxLoginFailures(5);
    connectionConfigFactory.setLoginFailureDelay(2000);
    serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
    BaseUser user = new BaseUser();
    user.setName(getNetworkConnection().getUserName());
    user.setPassword(getNetworkConnection().getPassword());
    user.setHomeDirectory(getNetworkConnection().getPath());
    List<Authority> list = new ArrayList<>();
    list.add(new WritePermission());
    list.add(new TransferRatePermission(0, 0));
    list.add(new ConcurrentLoginPermission(10, 10));
    user.setAuthorities(list);
    try {
        serverFactory.getUserManager().save(user);
    } catch (FtpException e) {
        CrashReportingManager.logException(e);
    }
    // do start server
    try {
        ftpServer = serverFactory.createServer();
        ftpServer.start();
        return true;
    } catch (Exception e) {
        ftpServer = null;
        handleServerStartError(e);
    }
    return false;
}
Also used : BaseUser(org.apache.ftpserver.usermanager.impl.BaseUser) Authority(org.apache.ftpserver.ftplet.Authority) ConcurrentLoginPermission(org.apache.ftpserver.usermanager.impl.ConcurrentLoginPermission) FtpServerFactory(org.apache.ftpserver.FtpServerFactory) ArrayList(java.util.ArrayList) TransferRatePermission(org.apache.ftpserver.usermanager.impl.TransferRatePermission) FtpException(org.apache.ftpserver.ftplet.FtpException) ConnectionConfigFactory(org.apache.ftpserver.ConnectionConfigFactory) WritePermission(org.apache.ftpserver.usermanager.impl.WritePermission) ListenerFactory(org.apache.ftpserver.listener.ListenerFactory) FtpException(org.apache.ftpserver.ftplet.FtpException)

Example 22 with FtpServerFactory

use of org.apache.ftpserver.FtpServerFactory in project TeachingInSimulation by ScOrPiOzzy.

the class ServerConfig method createFtpServer.

@Bean
public FtpServer createFtpServer() {
    // Apache Ftp Server 默认的编码是"UTF-8"
    FtpServerFactory serverFactory = new FtpServerFactory();
    // FTP服务连接配置
    ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();
    // 允许匿名连接
    connectionConfigFactory.setAnonymousLoginEnabled(true);
    serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
    // 
    serverFactory.setUserManager(userManager);
    // 配置FTP端口
    ListenerFactory listenerFactory = new ListenerFactory();
    // 默认21, 可以按需配置
    listenerFactory.setPort(ftpPort);
    serverFactory.addListener("default", listenerFactory.createListener());
    return serverFactory.createServer();
}
Also used : FtpServerFactory(org.apache.ftpserver.FtpServerFactory) ConnectionConfigFactory(org.apache.ftpserver.ConnectionConfigFactory) ListenerFactory(org.apache.ftpserver.listener.ListenerFactory) Bean(org.springframework.context.annotation.Bean)

Aggregations

FtpServerFactory (org.apache.ftpserver.FtpServerFactory)22 ListenerFactory (org.apache.ftpserver.listener.ListenerFactory)20 UserManager (org.apache.ftpserver.ftplet.UserManager)13 File (java.io.File)11 NativeFileSystemFactory (org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory)8 ClearTextPasswordEncryptor (org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor)8 BaseUser (org.apache.ftpserver.usermanager.impl.BaseUser)7 PropertiesUserManagerFactory (org.apache.ftpserver.usermanager.PropertiesUserManagerFactory)6 ConnectionConfigFactory (org.apache.ftpserver.ConnectionConfigFactory)5 PropertiesUserManager (org.apache.ftpserver.usermanager.impl.PropertiesUserManager)5 WritePermission (org.apache.ftpserver.usermanager.impl.WritePermission)5 ArrayList (java.util.ArrayList)4 Authority (org.apache.ftpserver.ftplet.Authority)4 SslConfigurationFactory (org.apache.ftpserver.ssl.SslConfigurationFactory)4 IOException (java.io.IOException)3 FtpException (org.apache.ftpserver.ftplet.FtpException)3 Listener (org.apache.ftpserver.listener.Listener)3 Before (org.junit.Before)3 BeforeClass (org.junit.BeforeClass)3 FtpServer (org.apache.ftpserver.FtpServer)2