Example usage for org.apache.commons.net.ftp FTPClient login

List of usage examples for org.apache.commons.net.ftp FTPClient login

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient login.

Prototype

public boolean login(String username, String password) throws IOException 

Source Link

Document

Login to the FTP server using the provided username and password.

Usage

From source file:ubic.basecode.util.NetUtils.java

/**
 * Convenient method to get a FTP connection.
 * //  ww  w.  j  ava2  s  .c  o m
 * @param host
 * @param login
 * @param password
 * @param mode
 * @return
 * @throws SocketException
 * @throws IOException
 */
public static FTPClient connect(int mode, String host, String loginName, String password)
        throws SocketException, IOException {
    FTPClient f = new FTPClient();
    f.enterLocalPassiveMode();
    f.setBufferSize(32 * 2 ^ 20);
    boolean success = false;
    f.connect(host);
    int reply = f.getReplyCode();
    if (FTPReply.isPositiveCompletion(reply))
        success = f.login(loginName, password);
    if (!success) {
        f.disconnect();
        throw new IOException("Couldn't connect to " + host);
    }
    f.setFileType(mode);
    log.debug("Connected to " + host);
    return f;
}

From source file:ubicrypt.core.provider.ftp.FTProvider.java

private Observable<FTPClient> connect() {
    return Observable.<FTPClient>create(subscriber -> {
        final FTPClient client = new FTPClient();
        try {/*from w w w  . j a  va 2 s  .  c  o  m*/
            client.connect(conf.getHost(), getConf().getPort() == -1 ? 21 : getConf().getPort());
            final int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                log.error("FTP server refused connection:" + client.getReplyString());
                if (client.isConnected()) {
                    client.disconnect();
                }
                subscriber.onError(
                        new RuntimeException("FTP server refused connection:" + client.getReplyString()));
                return;
            }
            if (!getConf().isAnonymous()) {
                if (!client.login(getConf().getUsername(), new String(getConf().getPassword()))) {
                    client.disconnect();
                    log.warn("FTP wrong credentials:" + client.getReplyString());
                    subscriber.onError(new RuntimeException("FTP wrong credentials"));
                }
            }
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.setBufferSize(1 << 64);
            client.enterLocalPassiveMode();
            client.setControlKeepAliveTimeout(60 * 60); //1h
            if (!isEmpty(conf.getFolder())) {
                final String directory = startsWith("/", conf.getFolder()) ? conf.getFolder()
                        : "/" + conf.getFolder();
                if (!client.changeWorkingDirectory(directory)) {
                    if (!client.makeDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                    if (!client.changeWorkingDirectory(directory)) {
                        disconnect(client);
                        subscriber.onError(new ProviderException(showServerReply(client)));
                        return;
                    }
                }
            }
            subscriber.onNext(client);
            subscriber.onCompleted();
        } catch (final IOException e) {
            disconnect(client);
            subscriber.onError(e);
        }
    }).subscribeOn(Schedulers.io());
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

/**
 * Do an FTP put of the given bytes//from  w  w w  .ja  v  a  2 s  . com
 *
 * @param server server
 * @param userName user name on server
 * @param password password on server
 * @param destination Where to put the bytes
 * @param bytes The bytes
 *
 * @throws Exception On badness
 */
public static void ftpPut(String server, String userName, String password, String destination, byte[] bytes)
        throws Exception {
    FTPClient f = new FTPClient();

    f.connect(server);
    f.login(userName, password);
    f.setFileType(FTP.BINARY_FILE_TYPE);
    f.enterLocalPassiveMode();
    checkFtp(f, "Connecting to ftp server");
    f.storeFile(destination, new ByteArrayInputStream(bytes));
    checkFtp(f, "Storing file");
    f.logout();
    f.disconnect();
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static FTPClient ftpConnect(String host, String username, String password) throws IOException {
    FTPClient ftp = new FTPClient();
    try {/*  w  w w.ja  v a2s.co  m*/

        ftp.connect(host);
        log.debug("Trying " + host);
        log.debug(ftp.getReplyString());
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new IOException("FTP server refused connection: " + reply);
        } else {
            log.info("Connected");
        }

        ftp.login(username, password);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    } catch (NoRouteToHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    } catch (UnknownHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    }
    return ftp;
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TransmissionUtils.java

public static FTPClient ftpConnect(String host, int port, String username, String password) throws IOException {
    FTPClient ftp = new FTPClient();
    try {/* ww  w  .jav  a2s. c  om*/
        ftp.connect(host, port);
        log.debug("Trying " + host + ":" + port);
        log.debug(ftp.getReplyString());
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new IOException("FTP server refused connection: " + reply);
        } else {
            log.debug("Connected");
        }

        ftp.login(username, password);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    } catch (NoRouteToHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    } catch (UnknownHostException e) {
        throw new IOException("Couldn't connect to printer: " + e.getMessage(), e);
    }
    return ftp;
}

From source file:uk.ac.manchester.cs.datadesc.validator.utils.UrlReader.java

private void login(FTPClient ftp) throws VoidValidatorException {
    try {//from   ww w .  j a  va 2s .  c o  m
        if (!ftp.login(username, password)) {
            ftp.logout();
        }
        logger.debug("Remote system is " + ftp.getSystemType());
    } catch (IOException ex) {
        disconnect(ftp);
        throw new VoidValidatorException(
                "Unable to log into FTP " + uri + " using username:" + username + " password:" + password);
    }
}

From source file:uk.sipperfly.utils.FTPUtil.java

public static void reconnect() throws SocketException, IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("UTF-8");
    ftpClient.connect(host, port);//from  www  .j a va  2  s. c  o  m
    ftpClient.login(username, password);
    if (mode.equalsIgnoreCase("passive")) {
        ftpClient.enterLocalPassiveMode();
    } else if (mode.equalsIgnoreCase("active")) {
        ftpClient.enterLocalActiveMode();
    }
    int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        Logger.getLogger(GACOM).log(Level.INFO, "FTP Login: ".concat(ftpClient.getReplyString()));
        ftpClient.disconnect();
    }
    ftpClient.setKeepAlive(true);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    ftpClient.setControlKeepAliveTimeout(300);
    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLKSIZE=27000");
    //      ftpClient.sendSiteCommand("CY");
    //      ftpClient.sendSiteCommand("PRI= 50");
    //      ftpClient.sendSiteCommand("SEC=25");

    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLOCKSIZE=27000");
    //      ftpClient.sendSiteCommand("SPACE=(CYL,(30,300),RLSE)"); 
    //      ftpClient.sendSiteCommand("TR");
    //      ftpClient.sendSiteCommand("PRI=450");
    //      ftpClient.sendSiteCommand("SEC=4500");

    Logger.getLogger(GACOM).log(Level.INFO, "Reconnected FTP");
    System.out.println("reconnected");
}

From source file:view.GenerujTest.java

private void sendfile() {

    FTPClient client = new FTPClient();
    FileInputStream fis = null;/*  w  w w  . j a  va2s.c o  m*/

    try {
        client.connect("ftp.serwer1749827.home.pl");
        client.login("serwer", "serwer123456");

        //
        // Create an InputStream of the file to be uploaded
        //
        client.removeDirectory("index.html");
        String filename = "index.html";
        fis = new FileInputStream(filename);

        //
        // Store file to server
        //
        client.storeFile(filename, fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:vv.main.java

private void btnBackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupActionPerformed
    // TODO add your handling code here:
    Conectar con = new Conectar();
    String nombreCarpeta = "FTF_" + con.getTime("dd-MM-yyyy") + "_" + con.getTime("HHmm");
    con.createDir("c:\\tmp\\" + nombreCarpeta);
    FTPClient client = new FTPClient();

    String sFTP = con.IP_FTP;/*from  w  ww. j a  va  2s  . co  m*/
    String sUser = con.USUARIO_FTP;
    String sPassword = con.PASSWORD_FTP;
    txtLog.setText("INICIO: " + con.getTime("dd-MM-yyyy HH:mm:ss"));
    try {
        client.connect(sFTP);
        boolean login = client.login(sUser, sPassword);
        con.copiarDir(client, nombreCarpeta);
        client.logout();
        client.disconnect();
    } catch (Exception e) {
        System.out.println("Error:" + e);
    }
    txtLog.setText(txtLog.getText() + "\nFIN: " + con.getTime("dd-MM-yyyy HH:mm:ss"));
}