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: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 {//from w w w  .j a va2  s.c o m
        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.taksmind.karma.functions.scheduled.Log.java

public static void uploadFile() throws IOException {
    try {// w  ww. j a va 2  s  .  com
        logDirectory = new File("logs");
        if (!logDirectory.isDirectory()) {
            logDirectory.mkdir();
        }
        date = sdf.parse(sdf.format(new Date())).toString().replaceAll(" ", "")
                .replaceAll("[0-9]+:[0-9]+:[0-9]+CDT", "");

        Properties properties = new Properties(); //creates configuration object
        properties.load(new FileInputStream(Main.configuration));
        ftpServer = properties.getProperty("ftpServer");
        ftpUser = properties.getProperty("ftpUser");
        ftpPassword = properties.getProperty("ftpPassword");

        client = new FTPClient();

        client.connect(ftpServer);
        int reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            System.err.println("FTP server refused connection.");
        }

        if (client.login(ftpUser, ftpPassword)) {
            client.enterLocalPassiveMode();
            String localFile = logDirectory.getAbsolutePath() + "/" + date + ".log";
            String remoteFile = "/downloads/logs/" + date + ".log";
            if (Main.debug) {
                System.out.println(localFile);
                System.out.println(remoteFile);
            }
            FileInputStream fis = new FileInputStream(new File(localFile));
            if (client.storeFile(remoteFile, fis)) {
                System.out.println("File uploaded successfully.");
            } else {
                System.err.println("Uploading file failed..");
            }
            fis.close();
            client.logout();
            client.disconnect();
        } else {
            System.out.println("Could not authenticate with FTP server..");
            client.logout();
        }

    } catch (ParseException e) {
        System.err.println("Error parsing log file");
        e.printStackTrace();
    } catch (SocketException e) {
        System.err.println("some exception happened.");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("can not open or upload file.");
        e.printStackTrace();
    } finally {
        if (client.isConnected()) {
            client.disconnect();
        }
    }
}

From source file:cn.zhuqi.mavenssh.web.util.FtpUtil.java

public FtpUtil(int defaultTimeoutSecond, int connectTimeoutSecond, int dataTimeoutSecond) {
    ftp = new FTPClient();
    is_connected = false;/*ww w  . j  ava 2  s  .co m*/

    ftp.setDefaultTimeout(defaultTimeoutSecond * 1000);
    ftp.setConnectTimeout(connectTimeoutSecond * 1000);
    ftp.setDataTimeout(dataTimeoutSecond * 1000);
}

From source file:ftpclient.FTPManager.java

public FTPManager(Settings settings) throws IOException {
    this.settings = settings;
    listeners = new ArrayList<>();
    client = new FTPClient();
}

From source file:it.unisannio.srss.dame.android.services.FTPService.java

public FTPService(String host, int port, String user, String pwd, boolean passive) {
    this.ftp = new FTPClient();
    this.url = host;
    this.user = user;
    this.pwd = pwd;
    this.port = port;
    this.passive = passive;
    // for debugging use
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
}

From source file:com.shigeodayo.ardrone.utils.ARDroneInfo.java

private boolean connectToDroneThroughFtp() {
    FTPClient client = new FTPClient();
    BufferedOutputStream bos = null;

    try {//from   www.  j a  va2  s  . c om
        client.connect(ARDroneConstants.IP_ADDRESS, ARDroneConstants.FTP_PORT);

        if (!client.login("anonymous", "")) {
            ARDrone.error("Login failed", this);
            return false;
        }

        client.setFileType(FTP.BINARY_FILE_TYPE);

        bos = new BufferedOutputStream(new OutputStream() {

            @Override
            public void write(int arg0) throws IOException {
                //System.out.println("aa:" + (char)arg0);
                switch (count) {
                case 0:
                    major = arg0 - '0';
                    break;
                case 2:
                    minor = arg0 - '0';
                    break;
                case 4:
                    revision = arg0 - '0';
                    break;
                default:
                    break;
                }
                count++;
            }
        });

        if (!client.retrieveFile("/" + VERSION_FILE_NAME, bos)) {
            ARDrone.error("Cannot find \"" + VERSION_FILE_NAME + "\"", this);
            return false;
        }

        bos.flush();

        //System.out.print("major:" + major);
        //System.out.print(" minor:" + minor);
        //System.out.println(" revision:" + revision);

        //System.out.println("done");
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (bos != null) {
                bos.flush();
                bos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}

From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static FTPClient connect(RepositoryLocation location, boolean showErrors) {

    FTPClient client = new FTPClient();
    String errorMessage = "";

    try {//from  www . j  a  v a2 s  . c  o  m
        client.connect(location.getHost());
        boolean ok = client.login(location.getUser(), location.getPassword());
        if (ok) {
            boolean ok2 = client.changeWorkingDirectory(location.getRepositoryPath());
            if (!ok2) {
                client = null;
                errorMessage = "Incorrect repository path";
            }
        } else {
            client = null;
            errorMessage = "Incorrect user name and password";
        }
    } catch (Exception e) {
        client = null;
        errorMessage = "Unknown host";
    }

    if (!errorMessage.equals("") && showErrors) {
        MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                "Connection failed", errorMessage);
    }

    return client;
}

From source file:com.adaptris.ftp.CommonsNetFtpClient.java

@Override
protected FTPClient createFTPClient() {
    return new FTPClient();
}

From source file:domain.FTP.java

public FTP() {

    server = properties.getProperty("serverftp");
    user = properties.getProperty("userftp");
    password = properties.getProperty("passwordftp");
    port = Integer.parseInt(properties.getProperty("portftp"));
    downloadpath = properties.getProperty("downloadpath");
    ftpClient = new FTPClient();
}

From source file:com.github.fge.ftpfs.io.commonsnetimpl.CommonsNetFtpAgent.java

public CommonsNetFtpAgent(final FtpAgentQueue queue, final FtpConfiguration cfg) {
    super(queue, cfg);
    ftpClient = new FTPClient();
    ftpClient.setAutodetectUTF8(true);//from   w  ww.  j a  v a2  s.com
}