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:ru.in360.FTPUploader.java

public boolean uploadFolder(File src, URI dest) {
    FTPClient ftpClient = new FTPClient();

    try {/*from  w  w w.  j a  v a2s  .c om*/
        // connect and login to the server
        ftpClient.connect(server, port);
        ftpClient.login(user, pass);

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

        System.out.println("Connected");

        String remoteDirPath = "/Upload";
        String localDirPath = "E:/winhex";

        FTPUtil.saveFilesToServer(ftpClient, remoteProject, src);

        // log out and disconnect from the server
        ftpClient.logout();
        ftpClient.disconnect();

        System.out.println("Disconnected");
        return true;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }
}

From source file:ru.in360.FTPUploader.java

public boolean upload(File src, URI dest) {
    FTPClient ftpClient = new FTPClient();

    try {//from www .  j  a v  a2 s  . c o  m
        // connect and login to the server
        ftpClient.connect(server, port);
        ftpClient.login(user, pass);

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

        String path = "";
        for (String dir : (remoteProject + dest.getPath()).split("/")) {
            ftpClient.makeDirectory(path + "/" + dir);
            path = path + "/" + dir;
        }

        System.out.println("Connected");

        System.out.println(remoteProject + dest.getPath());
        FTPUtil.saveFilesToServer(ftpClient, remoteProject + dest.getPath(), src);
        // log out and disconnect from the server
        ftpClient.logout();
        ftpClient.disconnect();

        System.out.println("Disconnected");
        return true;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }
}

From source file:ru.sbt.bit.ood.hw1.download.FTPDownloader.java

public FTPDownloader(String hostName, int port, String login, String password) {
    this.hostName = hostName;
    this.port = port;
    this.login = login;
    this.password = password;
    ftpClient = new FTPClient();
}

From source file:ruige.GrabFTPDateManager.java

private static Boolean moveFile(String tempPath, String userName, String passWord) throws Exception {
    FTPFunc ftpFunc = new FTPFunc();
    FTPClient ftp = new FTPClient();//FTP
    try {/*w w  w. j  a  va 2s.c  o m*/
        Boolean isConnect = ftpFunc.getConnectionFTP("61.152.176.30", 21, userName, passWord, "./recv", ftp);
        //             Boolean isConnect = ftpFunc.getConnectionFTP("121.40.65.100",21,userName,passWord,"./recv",ftp);
        if (true == isConnect) {//
            ArrayList<String> fileNameList = ftpFunc.getFTPFileList(ftp);
            if (null != fileNameList) {//
                for (int i = 0; i < fileNameList.size(); i++) {//FTP
                    Boolean isDownload = ftpFunc.downloadFromFTP(tempPath, fileNameList.get(i), ftp);//FTP
                    if (true == isDownload) {
                        Boolean isDelete = ftpFunc.deleteFromFTP("/recv/", fileNameList.get(i), ftp);//FTP
                        if (false == isDelete) {
                            return false;//false
                        }
                    } else {
                        return false;//false
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:s32a.Client.Startup.FTPHandler.java

/**
 * Checks whether client was able to login with given info.
 *
 * @return//from w  ww . j  a  v a 2 s  .co  m
 */
public boolean checkLogin() {
    boolean success = false;
    FTPClient client = null;
    try {
        if (SSL) {
            client = new FTPSClient(false);
        } else {
            client = new FTPClient();
        }

        client.connect(this.ftpServer);
        success = client.login(username, password);
    } catch (IOException ex) {
        success = false;
        Logger.getLogger(FTPHandler.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (client != null) {
            try {
                client.logout();
            } catch (IOException ex) {
                Logger.getLogger(FTPHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    return success;
}

From source file:s32a.Client.Startup.FTPHandler.java

/**
 * Retrieves server and codebase info from FTP server.
 * Codebase will need to be queried separately afterwards.
 * @return /*from  ww w. ja v  a 2  s. c  o m*/
 */
public List<ServerInfo> getFTPData() {
    FTPClient client = null;
    FileInputStream fis = null;
    FileOutputStream fos = null;
    List<ServerInfo> output = new ArrayList<>();

    if (SSL) {
        client = new FTPSClient(false);
    } else {
        client = new FTPClient();
    }

    try {
        System.out.println("connecting");
        client.connect(ftpServer);
        boolean login = client.login(this.username, this.password);
        System.out.println("login: " + login);
        client.enterLocalPassiveMode();

        // Reads codebase file from server
        File codebase = new File("codebase.properties");
        fos = new FileOutputStream(codebase.getAbsolutePath());
        client.retrieveFile("/Airhockey/Codebase/codebase.properties", fos);
        fos.close();
        this.codebaseURL = this.readCodebaseInfo(codebase);

        // Retrieves all currently active files from server
        File server = null;
        for (FTPFile f : client.listFiles("/Airhockey/Servers")) {
            server = new File(f.getName());
            fos = new FileOutputStream(server);
            client.retrieveFile("/Airhockey/Servers/" + f.getName(), fos);
            fos.close();
            output.add(this.readServerFile(server));
        }
        //Removes null entries
        output.remove(null);

        client.logout();
    } catch (IOException ex) {
        System.out.println("IOException: " + ex.getMessage());
        ex.printStackTrace();
    } catch (Exception ex) {
        System.out.println("exception caught: " + ex.getMessage());
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return output;
}

From source file:s32a.CodebaseDeployer.java

/**
 * Uploads given files to ftp server./*from ww  w.ja  v  a  2 s .  c o  m*/
 *
 * @param input key: desired name on server, Value: file to upload.
 */
private void uploadFiles(Map<String, File> input) {

    FTPClient client = null;
    if (SSL) {
        client = new FTPSClient(false);
    } else {
        client = new FTPClient();
    }
    FileInputStream fis = null;
    FileOutputStream fos = null;

    try {
        System.out.println("connecting");
        client.connect(ftpServer);
        boolean login = client.login(this.userName, this.password);
        System.out.println("login: " + login);
        client.enterLocalPassiveMode();

        //            client.setFileType(FTP.ASCII_FILE_TYPE);

        //Creates all directories required on the server
        System.out.println("creating directories");
        client.makeDirectory("Airhockey");
        client.makeDirectory("Airhockey/Codebase");
        client.makeDirectory("Airhockey/Servers");
        client.makeDirectory("Airhockey/Codebase/s32a");
        System.out.println("default directories made");
        for (String s : directories) {
            client.makeDirectory(s);
        }

        //Uploads codebase URL
        fis = new FileInputStream(this.codebaseFile);
        boolean stored = client.storeFile("Airhockey/Codebase/codebase.properties", fis);
        //            client.completePendingCommand();
        System.out.println("Stored codebase file: " + stored);
        fis.close();

        // Removes references to all servers
        for (FTPFile f : client.listFiles("Airhockey/Servers")) {
            if (f.isFile()) {
                System.out.println("Deleting Server Listing: " + f.getName());
                client.deleteFile("/Airhockey/Servers/" + f.getName());
            }
        }

        // Uploads all class files
        System.out.println("Uploading classes");
        String defaultLoc = fs + "Airhockey" + fs + "Codebase" + fs;
        for (String dest : input.keySet()) {
            fis = new FileInputStream(input.get(dest));
            if (!client.storeFile(defaultLoc + dest, fis)) {
                System.out.println("unable to save: " + defaultLoc + dest);
            }
            fis.close();
            //                client.completePendingCommand();
        }

        client.logout();
    } catch (IOException ex) {
        System.out.println("IOException: " + ex.getMessage());
        ex.printStackTrace();
    } catch (Exception ex) {
        System.out.println("exception caught: " + ex.getMessage());
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:s32a.FTPTest.java

public FTPTest(boolean login) {

    FTPClient client = new FTPClient();
    FileInputStream fis = null;/*from   w ww  .ja  v  a  2 s . c om*/
    FileOutputStream fos = null;

    try {
        client.connect("s32a.Airhockey.org");
        client.login("testey", "test");
        client.enterLocalPassiveMode();
        client.setFileType(FTP.ASCII_FILE_TYPE);

        client.makeDirectory("/testey");

        //        String filename = "testey.txt";
        //        File file = new File(filename);
        //        file.createNewFile();
    } catch (IOException ex) {
        Logger.getLogger(FTPTest.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:s32a.Server.Startup.FTPHandler.java

/**
 * Checks whether client was able to login with given info.
 *
 * @return Returns a boolean indicating whether the client has been
 * successfully logged in/*from  w ww  . ja  v a 2s . co  m*/
 */
public boolean checkLogin() {
    boolean success = false;
    FTPClient client = null;
    try {
        if (SSL) {
            client = new FTPSClient(false);
        } else {
            client = new FTPClient();
        }

        client.connect(this.ftpServer);
        success = client.login(username, password);
    } catch (IOException ex) {
        success = false;
        showDialog("Error", "FTP: CheckLogin IOException: " + ex.getMessage());
        //Logger.getLogger(FTPHandler.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (client != null) {
            try {
                client.logout();
            } catch (IOException ex) {
                showDialog("Error", "FTP: CheckLogin Logout IOException: " + ex.getMessage());
                //Logger.getLogger(FTPHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    return success;
}

From source file:s32a.Server.Startup.FTPHandler.java

/**
 * Registers//w w  w.j  a v  a 2s. com
 *
 * @param input The server info to be registered
 * @return The url that should be used as java for codebase purposes
 */
public String registerServer(ServerInfo input) {
    File infoFile = this.saveInfoToFile(input);
    if (infoFile == null || infoFile.length() == 0) {
        showDialog("Error", "No file to store: " + infoFile.getAbsolutePath());
        //System.out.println("No file to store: " + infoFile.getAbsolutePath());
        return null;
    }

    FTPClient client = null;
    FileInputStream fis = null;
    FileOutputStream fos = null;
    String output = null;

    if (SSL) {
        client = new FTPSClient(false);
    } else {
        client = new FTPClient();
    }

    try {
        System.out.println("connecting");
        client.connect(ftpServer);
        boolean login = client.login(this.username, this.password);
        System.out.println("login: " + login);
        client.enterLocalPassiveMode();

        fis = new FileInputStream(infoFile);
        this.ftpRefLocation = "/Airhockey/Servers/" + input.getIP() + "-" + input.getBindingName() + ".server";
        client.storeFile(this.ftpRefLocation, fis);

        File codebase = new File("codebase.properties");
        fos = new FileOutputStream(codebase.getAbsolutePath());
        client.retrieveFile("/Airhockey/Codebase/codebase.properties", fos);
        fos.close();
        output = this.readCodebaseInfo(codebase);

        client.logout();
    } catch (IOException ex) {
        showDialog("Error", "FTP: IOException " + ex.getMessage());
        //            System.out.println("IOException: " + ex.getMessage());
        //            ex.printStackTrace();
    } catch (Exception ex) {
        showDialog("Error", "FTP: Exception: " + ex.getMessage());
        //System.out.println("exception caught: " + ex.getMessage());
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            infoFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return output;
}