Barra laterale

programmazione:java:embed_apache_ftpserver

Includere Apache FtpServer nei nostri progetti Java

Autore: Fabio Di Matteo
Ultima revisione: 31/12/2017 - 14:46

Includere nei nostri progetti Java un server FTP . Tralascio come includere i file jar della nostra libreria.

package javaftpd;
 
/**
 *
 * @author fabio
 */
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.ftpserver.ConnectionConfigFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
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.WritePermission;
 
public class JavaFtpd {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FtpException {
        // TODO code application logic here
        FtpServerFactory serverFactory = new FtpServerFactory();
        FtpServer server = serverFactory.createServer();
        ListenerFactory factory = new ListenerFactory();
        factory.setPort(2221); //la porta
        serverFactory.addListener("default", factory.createListener());
 
 
        //Utente fabio
        ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();
        connectionConfigFactory.setAnonymousLoginEnabled(true); //per utente anonimo
        serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
        BaseUser user = new BaseUser();
        user.setName("fabio");
        user.setPassword("pass");
        user.setHomeDirectory("/home/fabio/Scaricati/");
 
        //permessi di scrittura sulla home
        List<Authority> authorities = new ArrayList<Authority>();
        authorities.add(new WritePermission());
        user.setAuthorities(authorities);
 
        //salvo le preferenze utente
        serverFactory.getUserManager().save(user);
 
 
 
 
        // start the server
        server.start();
    }
 
}

programmazione/java/embed_apache_ftpserver.txt · Ultima modifica: 18/04/2018 - 15:49 (modifica esterna)