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

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

Introduction

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

Prototype

public FTPClient() 

Source Link

Document

Default FTPClient constructor.

Usage

From source file:org.opennms.systemreport.formatters.FtpSystemReportFormatter.java

@Override
public void end() {
    m_zipFormatter.end();/* w  w  w .ja v a  2  s. c  om*/
    IOUtils.closeQuietly(m_outputStream);

    final FTPClient ftp = new FTPClient();
    FileInputStream fis = null;
    try {
        if (m_url.getPort() == -1 || m_url.getPort() == 0 || m_url.getPort() == m_url.getDefaultPort()) {
            ftp.connect(m_url.getHost());
        } else {
            ftp.connect(m_url.getHost(), m_url.getPort());
        }
        if (m_url.getUserInfo() != null && m_url.getUserInfo().length() > 0) {
            final String[] userInfo = m_url.getUserInfo().split(":", 2);
            ftp.login(userInfo[0], userInfo[1]);
        } else {
            ftp.login("anonymous", "opennmsftp@");
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            LOG.error("FTP server refused connection.");
            return;
        }

        String path = m_url.getPath();
        if (path.endsWith("/")) {
            LOG.error("Your FTP URL must specify a filename.");
            return;
        }
        File f = new File(path);
        path = f.getParent();
        if (!ftp.changeWorkingDirectory(path)) {
            LOG.info("unable to change working directory to {}", path);
            return;
        }
        LOG.info("uploading {} to {}", f.getName(), path);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        fis = new FileInputStream(m_zipFile);
        if (!ftp.storeFile(f.getName(), fis)) {
            LOG.info("unable to store file");
            return;
        }
        LOG.info("finished uploading");
    } catch (final Exception e) {
        LOG.error("Unable to FTP file to {}", m_url, e);
    } finally {
        IOUtils.closeQuietly(fis);
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
                // do nothing
            }
        }
    }
}

From source file:org.openo.commontosca.catalog.ftp.FtpUtil.java

/**
 * get ftp connection.//from  w  w w.  j a  va  2 s.  c  om
 * 
 * @param conftp Ftp
 * @return boolean
 * @throws Exception e
 */
public static boolean connectFtp(Ftp conftp) throws Exception {
    ftp = new FTPClient();
    boolean flag = false;
    if (conftp.getPort() == null) {
        ftp.connect(conftp.getIpAddr(), 21);
    } else {
        ftp.connect(conftp.getIpAddr(), conftp.getPort());
    }
    ftp.login(conftp.getUserName(), conftp.getPwd());
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return flag;
    }
    ftp.changeWorkingDirectory(conftp.getPath());
    flag = true;
    return flag;
}

From source file:org.openo.nfvo.emsdriver.commons.ftp.FTPSrv.java

/**
 * login FTP//from  w ww .  ja v a  2  s  .com
 * @param host
 * @param port
 * @param user
 * @param pwd
 * @param encode
 * @param timeout
 * @throws Exception
 */
public void login(String host, int port, String user, String pwd, String encode, boolean isPassiveMode,
        int timeout) throws Exception {
    ftpClient = new FTPClient();

    FTPClientConfig ftpClientConfig = new FTPClientConfig();
    ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
    this.ftpClient.setControlEncoding("GBK");
    this.ftpClient.configure(ftpClientConfig);
    ftpClient.setParserFactory(new ExtendsDefaultFTPFileEntryParserFactory());

    if (encode != null && encode.length() > 0) {
        ftpClient.setControlEncoding(encode);
    }

    ftpClient.connect(host, port);
    int reply = this.ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        this.ftpClient.disconnect();
        return;
    }

    if (!ftpClient.login(user, pwd)) {
        throw new Exception("login[" + host + "],port[" + port + "] fail, please check user and password");
    }
    if (isPassiveMode) {
        ftpClient.enterLocalPassiveMode();
    } else {
        ftpClient.enterLocalActiveMode();
    }
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    this.ftpClient.setBufferSize(1024 * 2);
    this.ftpClient.setDataTimeout(3 * 60 * 1000);
    try {
        this.ftpClient.setSoTimeout(timeout);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.opensutils.io.ftp.FTPApacheCommonsNetImpl.java

/**
 * Construtor<br>/*from  w  w  w  .  j  a v a2  s  . c o  m*/
 * @param host - ip or name the ftp-host for been connect
 * @param user - name user por login to ftp
 * @param password - password for login in ftp
 * @author Felipe Priuli
 */
public FTPApacheCommonsNetImpl(String host, String user, String password) {
    this.ftp = new FTPClient();
    this.fileType = FTP.FILE_TYPE.BINARY;
    this.hostName = host;
    this.user = user;
    this.password = password;
    this.attemptsCount = 2;

}

From source file:org.oss.bonita.utils.ftp.FtpTransfer.java

public FtpTransfer(String host, String user, String pwd, boolean debug) throws IOException {
    ftp = new FTPClient();
    if (debug) {//  www . j av a2s .  co  m
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    }
    int reply;
    ftp.connect(host);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new IOException("Connection refused: " + ftp.getReplyString());
    }
    if (!ftp.login(user, pwd)) {
        throw new IOException("Login failed");

    }
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();
    LOGGER.finest("FTP Connected to " + host);
}

From source file:org.oss.digitalforms.MobileFormsImpl.java

private void ftpValidations(String ftpHost, String user, String password) throws Exception {
    FTPClient ftp = new FTPClient();
    ftp.connect(ftpHost);/*from   w w w  .jav  a  2  s  .c o  m*/
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftp.login(user, password);
    ftp.listFiles();
    ftp.disconnect();
}

From source file:org.oss.digitalforms.utils.ftp.FtpTransfer.java

public FtpTransfer(String host, String user, String pwd, boolean debug) throws IOException {
    ftp = new FTPClient();
    if (debug) {//from w  ww  .  j a va2s.  c o  m
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    }
    int reply;
    ftp.connect(host);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new IOException("Connection refused: " + ftp.getReplyString());
    }
    if (!ftp.login(user, pwd)) {
        throw new IOException("Login failed");

    }
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();
    LOGGER.info("FTP Connected to " + host);
}

From source file:org.ow2.proactive.scheduler.examples.FTPConnector.java

/**
 * @see JavaExecutable#execute(TaskResult[])
 */// w w w  .  jav a2s. c  om
@Override
public Serializable execute(TaskResult... results) throws IOException {
    List<String> filesRelativePathName;
    FTPClient ftpClient = new FTPClient();

    try {
        //connect to the server
        ftpClient.connect(ftpHostname, ftpPort);
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            throw new IOException("Exception in connecting to FTP Server");
        }
        //login to the server
        if (!ftpClient.login(ftpUsername, ftpPassword)) {
            throw new IOException("Logging refused. check the FTP_USERNAME and FTP_PASSWORD values.");
        }

        // use local passive mode to pass firewall
        ftpClient.enterLocalPassiveMode();

        getOut().println("Connected");

        switch (ftpMode) {
        //FTP mode is GET
        case GET:
            filesRelativePathName = ftpGet(ftpClient);
            break;

        //FTP mode is PUT
        case "PUT":
            filesRelativePathName = ftpPut(ftpClient);
            break;

        default:
            throw new IllegalArgumentException("FTP MODE can only be " + PUT + " or " + GET);

        }
    } finally {
        // log out and disconnect from the server
        ftpClient.logout();
        ftpClient.disconnect();
        getOut().println("Disconnected");
    }
    return (Serializable) filesRelativePathName;
}

From source file:org.ow2.proactive.scripting.helper.filetransfer.driver.FTP_VFS_Driver.java

/************************************************************************
 * Connect and disconnect methods/*w w  w . j  av  a 2  s .  com*/
 ************************************************************************/
private void connect() throws Exception {
    //--Connection
    ftpClient = new FTPClient();
    ftpClient.connect(host, port);

    //--Get reply code to check if FTP connection is setup
    int replyCode = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        ftpClient.disconnect();
        throw new Exception("FTP server refused connection.");
    }

    debug("Connected to remote host " + host);

    //--Login using user and password
    if (!ftpClient.login(user, pass)) {
        throw new AuthentificationFailedException(
                "Username and password do not match. Could not login to ftp server at " + host);
    }

    if (useBinaryMode) {
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        debug("Filetype set to binary type");
    } else {
        ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
        debug("Filetype set to ascii type");
    }

    if (usePassiveMode) {

        ftpClient.enterLocalPassiveMode();
        debug("Enetring passive mode");
    }
}

From source file:org.paxle.crawler.ftp.impl.FtpUrlConnection.java

protected FtpUrlConnection(URL url) throws URISyntaxException {
    super(url);//from  w w  w  .  j a v a2  s .c  o  m
    this.uri = url.toURI();
    this.client = new FTPClient();
    this.setConnectTimeout(15000);
    this.setReadTimeout(15000);
}