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:de.l3s.dlg.ncbikraken.ftp.NcbiFTPClient.java

public static void getMedline(MedlineFileType type) {
    FileOutputStream out = null;//from  w ww  .  j av  a  2 s . c om
    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 {/*  w  w  w  .  jav  a  2s  .c  o  m*/
        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;

}

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);/*from w  ww .jav a 2  s . c o  m*/
    int replyCode = client.getReplyCode();

    if (FTPReply.isPositiveCompletion(replyCode)) {
        try {
            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:com.kcs.core.utilities.FtpUtil.java

public static FTPClient openFtpConnect(String hostname, int port, String username, String password)
        throws Exception {
    FTPClient ftp = null;
    try {//w  w  w.jav a  2 s .c  om
        ftp = new FTPClient();
        ftp.setConnectTimeout(1000);
        ftp.connect(hostname, port);
        ftp.login(username, password);
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return ftp;
}

From source file:joshuatee.wx.UtilityFTP.java

public static String[] GetNidsArr(Context c, String url, String path, String frame_cnt_str) {

    int frame_cnt = Integer.parseInt(frame_cnt_str);
    String[] nids_arr = new String[frame_cnt];

    try {//w w  w .  j  a  v a2  s  .  com
        FTPClient ftp = new FTPClient();

        //String user = "ftp";
        //String pass = "anonymous";

        ftp.connect(url);

        if (!ftp.login("ftp", "anonymous")) {
            ftp.logout();
        }

        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }

        ftp.enterLocalPassiveMode();
        ftp.changeWorkingDirectory(path);
        //reply = ftp.getReplyCode();

        FTPFile[] ftpFiles = ftp.listFiles();

        //get newest .xml file name from ftp server
        java.util.Date lastMod = ftpFiles[0].getTimestamp().getTime();
        FTPFile choice = ftpFiles[0];

        for (FTPFile file : ftpFiles) {
            if (file.getTimestamp().getTime().after(lastMod) && !file.getName().equals("sn.last")) {
                choice = file;
                lastMod = file.getTimestamp().getTime();
            }
        }

        int seq = Integer.parseInt(choice.getName().replace("sn.", "")); // was ALl
        int j = 0;
        int k = seq - frame_cnt + 1;
        for (j = 0; j < frame_cnt; j++) {
            // files range from 0000 to 0250, if num is negative add 251
            int tmp_k = k;

            if (tmp_k < 0)
                tmp_k = tmp_k + 251;

            nids_arr[j] = "sn." + String.format("%4s", Integer.toString(tmp_k)).replace(' ', '0');
            k++;
        }

        FileOutputStream fos;
        for (j = 0; j < frame_cnt; j++) {
            fos = c.openFileOutput(nids_arr[j], Context.MODE_PRIVATE);
            ftp.retrieveFile(nids_arr[j], fos);
            fos.close();
        }

        ftp.logout();
        ftp.disconnect();

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

    return nids_arr;

}

From source file:com.microsoft.azure.management.appservice.samples.ManageWebAppSourceControl.java

private static void uploadFileToFtp(PublishingProfile profile, String fileName, InputStream file)
        throws Exception {
    FTPClient ftpClient = new FTPClient();
    String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
    String server = ftpUrlSegments[0];
    String path = "./site/wwwroot/webapps";
    ftpClient.connect(server);//w w  w.j av  a2 s. c om
    ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.changeWorkingDirectory(path);
    ftpClient.storeFile(fileName, file);
    ftpClient.disconnect();
}

From source file:cn.zhuqi.mavenssh.web.util.test.FtpTest.java

/**
 * Description: FTP?/*from  w w  w .  j a  v  a  2 s . c  o m*/
 *
 * @Version1.0
 * @param url FTP?hostname
 * @param port FTP??
 * @param username FTP?
 * @param password FTP?
 * @param remotePath FTP?
 * @param fileName ???
 * @param localPath ??
 * @return
 */
public static boolean downloadFile(String url, int port, String username, String password, String remotePath,
        String fileName, String localPath) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        // FTP?
        if (port > -1) {
            ftp.connect(url, port);
        } else {
            ftp.connect(url);
        }
        ftp.login(username, password);//
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(remotePath);//FTP?
        FTPFile[] fs = ftp.listFiles();
        for (FTPFile ff : fs) {
            if (ff.getName().equals(fileName)) {
                File localFile = new File(localPath + "/" + ff.getName());
                OutputStream is = new FileOutputStream(localFile);
                ftp.retrieveFile(ff.getName(), is);
                is.close();
            }
        }

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

From source file:joshuatee.wx.UtilityFTP.java

public static void GetNids(Context c, String url, String path) {

    try {/*from   w w w  . j  ava2s. com*/
        FTPClient ftp = new FTPClient();

        //String url = "tgftp.nws.noaa.gov";
        //String user = "ftp";
        //String pass = "anonymous";

        ftp.connect(url);

        if (!ftp.login("ftp", "anonymous")) {
            ftp.logout();
        }

        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }

        ftp.enterLocalPassiveMode();
        ftp.changeWorkingDirectory(path);
        //reply = ftp.getReplyCode();
        //String fn = "sn.last";

        FileOutputStream fos = c.openFileOutput("nids", Context.MODE_PRIVATE);
        ftp.retrieveFile("sn.last", fos);
        //reply = ftp.getReplyCode();
        fos.close();

        ftp.logout();
        ftp.disconnect();

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

From source file:cn.zhuqi.mavenssh.web.util.test.FtpTest.java

/**
 * Description: ?FTP?/*from   w w w . jav  a  2s  . c o m*/
 *
 * @param url FTP?hostname
 * @param port FTP???-1
 * @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?
        if (port > -1) {
            ftp.connect(url, port);
        } else {
            ftp.connect(url);
        }
        // FTP
        ftp.login(username, password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        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) {
        success = false;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
            }
        }
    }
    return success;
}

From source file:com.intellij.diagnostic.SubmitPerformanceReportAction.java

@Nullable
private static String uploadFileToFTP(final File reportPath, @NonNls final String ftpSite,
        @NonNls final String directory, final ProgressIndicator indicator) {
    FTPClient ftp = new FTPClient();
    ftp.setConnectTimeout(30 * 1000);//from  w  w  w.ja  va2 s . co  m
    try {
        indicator.setText("Connecting to server...");
        ftp.connect(ftpSite);
        indicator.setText("Connected to server");

        if (!ftp.login("anonymous", "anonymous@jetbrains.com")) {
            return "Failed to login";
        }
        indicator.setText("Logged in");

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

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return "FTP server refused connection: " + reply;
        }
        if (!ftp.changeWorkingDirectory(directory)) {
            return "Failed to change directory";
        }

        // else won't work behind FW
        ftp.enterLocalPassiveMode();

        if (!ftp.setFileType(FTPClient.BINARY_FILE_TYPE)) {
            return "Failed to switch to binary mode";
        }

        indicator.setText("Transferring (" + StringUtil.formatFileSize(reportPath.length()) + ")");
        FileInputStream readStream = new FileInputStream(reportPath);
        try {
            if (!ftp.storeFile(reportPath.getName(), readStream)) {
                return "Failed to upload file";
            }
        } catch (IOException e) {
            return "Error during transfer: " + e.getMessage();
        } finally {
            readStream.close();
        }
        ftp.logout();
        return null;
    } catch (IOException e) {
        return "Failed to upload: " + e.getMessage();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
                // do nothing
            }
        }
    }
}