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:com.ephesoft.dcma.util.FTPUtil.java

/**
 * API for creating connection to ftp server.
 * /*  w w  w .  j av  a  2s . co  m*/
 * @param client {@link FTPClient} the ftp client instance
 * @throws SocketException generate if any error occurs while making the connection.
 * @throws IOException generate if any error occur while making the connection.
 */
public static void createConnection(final FTPClient client, final String ftpServerURL, final String ftpUsername,
        final String ftpPassword, final String ftpDataTimeOut) throws SocketException, IOException {
    client.connect(ftpServerURL);
    client.login(ftpUsername, ftpPassword);
    try {
        int ftpDataTimeOutLocal = Integer.parseInt(ftpDataTimeOut);
        client.setDataTimeout(ftpDataTimeOutLocal);
    } catch (NumberFormatException e) {
        LOGGER.error(EphesoftStringUtil.concatenate("Error occuring in converting ftpDataTimeOut :",
                e.getMessage(), e));
    }
}

From source file:eu.ggnet.dwoss.misc.op.listings.FtpTransfer.java

/**
 * Uploads a some files to a remove ftp host.
 * <p/>// w w  w  . j  a  v a 2  s  . c o m
 * @param config  the config for he connection.
 * @param uploads the upload commands
 * @param monitor an optional monitor.
 * @throws java.net.SocketException
 * @throws java.io.IOException
 */
public static void upload(ConnectionConfig config, IMonitor monitor, UploadCommand... uploads)
        throws SocketException, IOException {
    if (uploads == null || uploads.length == 0)
        return;
    SubMonitor m = SubMonitor.convert(monitor, "FTP Transfer", toWorkSize(uploads) + 4);
    m.message("verbinde");
    m.start();
    FTPClient ftp = new FTPClient();
    ftp.addProtocolCommandListener(PROTOCOL_TO_LOGGER);

    try {
        ftp.connect(config.getHost(), config.getPort());
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))
            throw new IOException("FTPReply.isPositiveCompletion(ftp.getReplyCode()) = false");
        if (!ftp.login(config.getUser(), config.getPass()))
            throw new IOException("Login with " + config.getUser() + " not successful");
        L.info("Connected to {} idenfied by {}", config.getHost(), ftp.getSystemType());

        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        for (UploadCommand upload : uploads) {
            m.worked(1, "uploading to " + upload.getPath());
            ftp.changeWorkingDirectory(upload.getPath());
            deleteFilesByType(ftp, upload.getDeleteFileTypes());
            for (File file : upload.getFiles()) {
                m.worked(1, "uploading to " + upload.getPath() + " file " + file.getName());
                try (InputStream input = new FileInputStream(file)) {
                    if (!ftp.storeFile(file.getName(), input))
                        throw new IOException("Cannot store file " + file + " on server!");
                }
            }
        }
        m.finish();
    } finally {
        // just cleaning up
        try {
            ftp.logout();
        } catch (IOException e) {
        }
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
            }
        }
    }
}

From source file:mysynopsis.FTPUploader.java

public static String uploadWizard() throws IOException {

    FTPClient connect;
    connect = null;/*from w ww .j av  a  2s.  co  m*/

    try {
        connect = new FTPClient();
        connect.connect(server, 21);
        int reply = connect.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            connect.disconnect();
            return "Can't Connect to the Server";
        }

        boolean check = connect.login(user, pass);
        if (!check) {
            return "Username or password Incorrect";
        }

        connect.setFileType(FTP.BINARY_FILE_TYPE);
        connect.enterLocalPassiveMode();

        InputStream input;
        try {
            input = new FileInputStream(new File("index.html"));
            connect.storeFile(dir + "index.html", input);
            input.close();
            connect.logout();
            connect.disconnect();

        } catch (IOException ex) {
            return "You need to put a slash (/) at the end";
        }
    } catch (IOException iOException) {
        return "Wrong Server Information. Please Try again";
    }
    return "File Transfer Successful!";
}

From source file:com.jason.thrift.TransmitHandler.java

/**
 * Description: ?FTP?/*from   w w w .ja  v a  2 s.  co m*/
 * 
 * @Version1.0 Jul 27, 2008 4:31:09 PM by ?cuihongbao@d-heaven.com
 * @param url
 *            FTP?hostname
 * @param port
 *            FTP??
 * @param username
 *            FTP?
 * @param password
 *            FTP?
 * @param path
 *            FTP??
 * @param filename
 *            FTP???
 * @param input
 *            ?
 * @return ?true?false
 */
public static boolean uploadFile(String url, int port, String username, String password, String path,
        String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        ftp.connect(url, port);// FTP?
        // ??ftp.connect(url)?FTP?
        ftp.login(username, password);// 
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(path);
        ftp.storeFile(filename, input);

        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
    return success;
}

From source file:AIR.Common.Web.FileFtpHandler.java

/**
 * Makes FTP call to the provided URI and retrieves contents
 *  //w ww  .  j  av  a2s .  co m
 * @param uri
 * @return ByteArrayInputStream
 * @throws FtpResourceException
 */
public static byte[] getBytes(URI uri) throws FtpResourceException {
    try {
        FTPClient ftp = new FTPClient();
        String[] credentialsAndHost = uri.getAuthority().split("@");
        String host = credentialsAndHost[1];
        String[] credentials = credentialsAndHost[0].split(":");

        ftp.connect(host);
        ftp.enterLocalPassiveMode();
        if (!ftp.login(credentials[0], credentials[1])) {
            ftp.logout();
            ftp.disconnect();
            throw new RuntimeException("FTP Authentication Failure");
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.logout();
            ftp.disconnect();
            throw new RuntimeException("FTP No reponse from server");
        }
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ftp.retrieveFile(uri.getPath(), output);
        output.close();
        ftp.logout();
        ftp.disconnect();
        return output.toByteArray();

    } catch (IOException ex) {
        throw new FtpResourceException(ex);
    }
}

From source file:facturacion.ftp.FtpServer.java

public static InputStream getFileInputStream(String remote_file_ruta) {
    FTPClient ftpClient = new FTPClient();
    try {/*from w ww  .  j a va2 s. co m*/
        ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken),
                Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken)));
        ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken),
                Config.getInstance().getProperty(Config.PassFtpToken));
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        //ftpClient.enterLocalPassiveMode();
        // crear directorio
        //OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(file_ruta));
        //System.out.println("File #1 has been downloaded successfully. 1");
        //FileInputStream fis = new FileInputStream("C:\\Users\\aaguerra\\Desktop\\firmado2.p12");
        //InputStream is = fis;
        System.out.println("File ruta=" + "1/" + remote_file_ruta);
        InputStream is = ftpClient.retrieveFileStream(remote_file_ruta);

        if (is == null)
            System.out.println("File #1 es null");
        else
            System.out.println("File #1 no es null");

        //return ftpClient.retrieveFileStream(remote_file_ruta);
        return is;
    } catch (IOException ex) {
        System.out.println("File #1 has been downloaded successfully. 222");
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            System.out.println("File #1 has been downloaded successfully. 3");
            return null;
            //ex.printStackTrace();
        }
    }
    return null;
}

From source file:com.glaf.core.util.FtpUtils.java

/**
 * //  w w  w  .  j  ava2 s .  co m
 * @param ip
 *            ???IP?
 * @param port
 *            ?
 * @param user
 *            ??
 * @param password
 *            ?
 */
public static FTPClient connectServer(String ip, int port, String user, String password) {
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(ip, port);
        ftpClient.login(user, password);
        ftpClientLocal.set(ftpClient);
        logger.info("login success!");
        return ftpClient;
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.error("login failed", ex);
        throw new RuntimeException(ex);
    }
}

From source file:com.glaf.core.util.FtpUtils.java

/**
 * ???FTP?/*from   www . j a v  a 2  s .  c  om*/
 */
public static FTPClient connectServer() {
    String ip = conf.get("ftp.host", "127.0.0.1");
    int port = conf.getInt("ftp.port", 21);
    String user = conf.get("ftp.user", "admin");
    String password = conf.get("ftp.password", "admin");
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(ip, port);
        ftpClient.login(user, password);
        ftpClientLocal.set(ftpClient);
        logger.info("login success!");
        return ftpClient;
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.error("login failed", ex);
        throw new RuntimeException(ex);
    }
}

From source file:com.glaf.core.util.FtpUtils.java

/**
 * ???FTP?//from www .j a  v  a 2 s.  co m
 */
public static FTPClient connectServer(String prefix) {
    String ip = conf.get(prefix + ".ftp.host", "127.0.0.1");
    int port = conf.getInt(prefix + ".ftp.port", 21);
    String user = conf.get(prefix + ".ftp.user", "admin");
    String password = conf.get(prefix + ".ftp.password", "admin");
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(ip, port);
        ftpClient.login(user, password);
        ftpClientLocal.set(ftpClient);
        logger.info("login success!");
        return ftpClient;
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.error("login failed", ex);
        throw new RuntimeException(ex);
    }
}

From source file:facturacion.ftp.FtpServer.java

public static int sendImgFile(String fileNameServer, String hostDirServer, InputStream localFile) {
    FTPClient ftpClient = new FTPClient();
    boolean success = false;
    BufferedInputStream buffIn = null;
    try {// w  w w.  j  a  v  a  2s  . com
        ftpClient.connect(Config.getInstance().getProperty(Config.ServerFtpToken),
                Integer.parseInt(Config.getInstance().getProperty(Config.PortFtpToken)));
        ftpClient.login(Config.getInstance().getProperty(Config.UserFtpToken),
                Config.getInstance().getProperty(Config.PassFtpToken));
        ftpClient.enterLocalPassiveMode();
        /*ftpClient.connect("127.0.0.1", 21);
          ftpClient.login("erpftp", "Tribut@2014");*/
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        int reply = ftpClient.getReplyCode();

        System.out.println("Respuesta recibida de conexin FTP:" + reply);

        if (!FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Imposible conectarse al servidor");
            return -1;
        }

        buffIn = new BufferedInputStream(localFile);//Ruta del archivo para enviar
        ftpClient.enterLocalPassiveMode();
        //crear directorio
        success = ftpClient.makeDirectory(hostDirServer);
        System.out.println("sucess 1 = " + success);
        success = ftpClient.makeDirectory(hostDirServer + "/img");
        System.out.println("sucess 233 = " + success);
        success = ftpClient.storeFile(hostDirServer + "/img/" + fileNameServer, buffIn);
        System.out.println("sucess 3 = " + success);
    } catch (IOException ex) {

    } finally {
        try {
            if (ftpClient.isConnected()) {
                buffIn.close(); //Cerrar envio de arcivos al FTP
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            return -1;
            //ex.printStackTrace();
        }
    }
    return (success) ? 1 : 0;
}