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

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

Introduction

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

Prototype

public void connect(InetAddress host) throws SocketException, IOException 

Source Link

Document

Opens a Socket connected to a remote host at the current default port and originating from the current host at a system assigned port.

Usage

From source file:com.clothcat.hpoolauto.HtmlUploader.java

/**
 * Upload the html via FTP//from  w w w. ja v  a2  s .c om
 */
public static void uploadHtml() {
    FTPClient ftpc = new FTPClient();
    FTPClientConfig conf = new FTPClientConfig();
    ftpc.configure(conf);
    try {
        ftpc.connect(Constants.FTP_HOSTNAME);
        ftpc.login(Constants.FTP_USERNAME, Constants.FTP_PASSWORD);
        File dir = new File(Constants.HTML_FILEPATH);
        File[] files = dir.listFiles();
        ftpc.changeWorkingDirectory("/htdocs");
        for (File f : files) {
            HLogger.log(Level.FINEST, "Uploading file: " + f.getName());
            FileInputStream fis = new FileInputStream(f);
            ftpc.storeFile(f.getName(), fis);
        }
        ftpc.logout();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (ftpc.isConnected()) {
            try {
                ftpc.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:ftp.FTPtask.java

/**
 * ? ? ? ??      Folder/*from w  ww.ja  v  a  2  s  . c o m*/
 * @param FTPADDR, ?  ?
 * @param user,   
 * @param password,   
 * @param Folder,    ?? ?
 * @return String[] result, ?? /
 */

public static String[] GetList(String FTPADDR, String user, String password, String Folder) {
    String[] result;
    result = null;

    FTPClient client = new FTPClient();

    try {
        client.connect(FTPADDR);
        client.login(user, password);
        client.changeWorkingDirectory(Folder);
        result = client.listNames();
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.igormaznitsa.zxpoly.utils.ROMLoader.java

static byte[] loadFTPArchive(final String host, final String path, final String name, final String password)
        throws IOException {
    final FTPClient client = new FTPClient();
    client.connect(host);
    int replyCode = client.getReplyCode();

    if (FTPReply.isPositiveCompletion(replyCode)) {
        try {/*from ww w  .  jav a 2 s . c  o  m*/
            client.login(name == null ? "" : name, password == null ? "" : password);
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.enterLocalPassiveMode();

            final ByteArrayOutputStream out = new ByteArrayOutputStream(300000);
            if (client.retrieveFile(path, out)) {
                return out.toByteArray();
            } else {
                throw new IOException(
                        "Can't load file 'ftp://" + host + path + "\' status=" + client.getReplyCode());
            }
        } finally {
            client.disconnect();
        }
    } else {
        client.disconnect();
        throw new IOException("Can't connect to ftp '" + host + "'");
    }
}

From source file:ftp.FTPtask.java

/**
 * Change directory//from   ww  w .  j  a v a2 s  . c o  m
 * @param FTPADDR, ?  ?
  * @param user,   
  * @param password,   
  * @param ChangeFolder, ,     ,  /upload
 */
public static void FTPChangeDir(String FTPADDR, String user, String password, String ChangeFolder) {

    FTPClient client = new FTPClient();

    try {

        client.connect(FTPADDR);
        client.login(user, password);

        int replyCode = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            System.out.println("Connect failed");
            return;
        }

        boolean success = client.login(user, password);
        showServerReply(client);

        if (!success) {
            System.out.println("Could not login to the server");
            return;
        }

        // Changes working directory
        success = client.changeWorkingDirectory(ChangeFolder);
        showServerReply(client);

        if (success) {
            System.out.println("Successfully changed working directory.");
        } else {
            System.out.println("Failed to change working directory. See server's reply.");
        }

        // logs out
        client.logout();
        client.disconnect();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.qasp.diego.arsp.Atualiza.java

public static boolean DownloadFTP() throws IOException {

    final String FTPURL = "37.187.45.24";
    final String USUARIO = "diego";
    final String SENHA = "Jogador5";
    final String ARQUIVO = "data.dat";
    FTPClient ftp = new FTPClient();

    try {// w  w  w .ja v  a  2  s.  c o  m
        ftp.connect(FTPURL);
        ftp.login(USUARIO, SENHA);
        ftp.changeWorkingDirectory("/httpdocs/tcc/TCCpython");
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        OutputStream outputStream = null;
        boolean downloadcomsucesso = false;
        try {
            File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                    ARQUIVO);
            f.createNewFile();
            outputStream = new BufferedOutputStream(new FileOutputStream(f));
            downloadcomsucesso = ftp.retrieveFile(ARQUIVO, outputStream);
        } finally {
            if (outputStream != null)
                outputStream.close();
        }
        return downloadcomsucesso;
    } finally {
        if (ftp != null) {
            ftp.logout();
            ftp.disconnect();
        }
    }
}

From source file:ftp.FTPtask.java

/**
 * Upload a file to a FTP server//  w w w  .java2 s. c  om
  * @param FTPADDR, ?  ?
  * @param user,   
  * @param password,   
 * @param PathOnFtp,      -  /upload/touch.dat
 * @param FilenameOnLocalMachine,       -  C:/somefile.txt
 */

public static void UploadFileOnFtp(String FTPADDR, String user, String password, String PathOnFtp,
        String FilenameOnLocalMachine) {
    FTPClient client = new FTPClient();

    FileInputStream fis = null;

    try {
        client.connect(FTPADDR);
        client.login(user, password);

        // Create an InputStream of the file to be uploaded

        String filename = FilenameOnLocalMachine;
        fis = new FileInputStream(filename);

        // Store file to server

        client.storeFile(PathOnFtp, 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:ftp.FTPtask.java

/**
 * Download File from FTP//from  www .j  a va 2s .  co m
  * @param FTPADDR, ?  ?
  * @param user,   
  * @param password,   
 * @param FullPathToPutFile -       ,    ??
 * @param FilenameOnFTP - ?    (?     /upload/)
 */

public static void DownloadFileFromFTP(String FTPADDR, String user, String password, String FullPathToPutFile,
        String FilenameOnFTP) {

    FTPClient client = new FTPClient();
    FileOutputStream fos = null;

    try {
        client.connect(FTPADDR);
        client.login(user, password);

        //       The remote filename to be downloaded.

        String filename = FullPathToPutFile;
        fos = new FileOutputStream(filename);

        //       Download file from FTP server

        client.retrieveFile("/upload/" + FilenameOnFTP, fos);
        //

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:de.jwi.ftp.FTPUploader.java

public static String upload(URL url, List files) {
    String rcs = "";

    if (!"ftp".equals(url.getProtocol())) {
        return "not ftp protocol";
    }//  w  w  w  .j a  v  a 2  s.  c om

    String host = url.getHost();
    String userInfo = url.getUserInfo();
    String path = url.getPath();
    String user = null;
    String pass = null;

    int p;
    if ((userInfo != null) && ((p = userInfo.indexOf(':')) > -1)) {
        user = userInfo.substring(0, p);
        pass = userInfo.substring(p + 1);
    } else {
        user = userInfo;
    }

    FTPClient ftp = new FTPClient();

    try {
        int reply;
        ftp.connect(host);

        // After connection attempt, you should check the reply code to verify
        // success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return "connection refused";
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        return "could not connect to " + host;
    }

    try {
        if (!ftp.login(user, pass)) {
            ftp.logout();
            return "failed to login";
        }

        ftp.setFileType(FTP.BINARY_FILE_TYPE);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        ftp.enterLocalPassiveMode();

        rcs = uploadFiles(ftp, path, files);

        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        return "connection closed";
    } catch (IOException e) {
        return e.getMessage();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    return rcs;
}

From source file:de.l3s.dlg.ncbikraken.ftp.NcbiFTPClient.java

public static void getMedline(MedlineFileType type) {
    FileOutputStream out = null;// w w w.ja v  a 2s  .c  o  m
    FTPClient ftp = new FTPClient();
    try {
        // Connection String
        LOGGER.info("Connecting to FTP server " + SERVER_NAME);
        ftp.connect(SERVER_NAME);
        ftp.login("anonymous", "");
        ftp.cwd(BASELINE_PATH);
        ftp.cwd(type.getServerPath());

        try {
            ftp.pasv();
        } catch (IOException e) {
            LOGGER.error(
                    "Can not access the passive mode. Maybe a problem with your (Windows) firewall. Just try to run as administrator: \nnetsh advfirewall set global StatefulFTP disable");
            return;
        }

        for (FTPFile file : ftp.listFiles()) {
            if (file.isFile()) {
                File meshF = new File(file.getName());
                LOGGER.debug("Downloading file: " + SERVER_NAME + ":" + BASELINE_PATH + "/"
                        + type.getServerPath() + "/" + meshF.getName());
                out = new FileOutputStream(Configuration.INSTANCE.getMedlineDir() + File.separator + meshF);
                ftp.retrieveFile(meshF.getName(), out);
                out.flush();
                out.close();
            }
        }

    } catch (IOException ioe) {
        LOGGER.error(ioe.getMessage());

    } finally {
        IOUtils.closeQuietly(out);
        try {
            ftp.disconnect();
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
    }

}

From source file:deincraftlauncher.IO.download.FTPSync.java

public static String[] listFiles(String dir) {

    FTPClient client = new FTPClient();

    try {/* ww  w. j  ava 2 s .com*/
        client.connect(ftpServer);
        client.login(ftpUsername, ftpPassword);
        client.changeWorkingDirectory(dir);

        // Obtain a list of filenames in the current working
        // directory. When no file found an empty array will 
        // be returned.
        String[] names = client.listNames();

        client.logout();

        return names;

    } catch (IOException e) {
        System.err.println("Error connecting to ftp (listfiles): " + e);
    } finally {

        try {
            client.disconnect();
        } catch (IOException e) {
            System.err.println("Error disconnecting to ftp (listfiles): " + e);
        }
    }

    return null;

}