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

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

Introduction

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

Prototype

public boolean logout() throws IOException 

Source Link

Document

Logout of the FTP server by sending the QUIT command.

Usage

From source file:rapture.ftp.common.FTPConnection.java

@Override
public void logoffAndDisconnect() {
    try {/*from  w ww.  j a v a  2 s  .c  o m*/
        FTPService.runWithRetry("Error closing connection", this, false, new FTPAction<Boolean>() {
            @Override
            public Boolean run(int attemptNum) throws IOException {
                log.debug(String.format("Disconnecting from %s. Attempt %s of %s", config.getAddress(),
                        attemptNum + 1, config.getRetryCount()));
                FTPClient ftpClient = getFtpClient();
                try {
                    if (isLoggedIn) {
                        ftpClient.logout();
                    }
                    return true;
                } catch (IOException ignore) {
                    return false;
                } finally {
                    ftpClient.disconnect();
                }
            }
        });
    } catch (RaptureException e) {
        log.warn("Unable to log off and disconnect, but will not die: " + ExceptionToString.format(e));
    }
}

From source file:rems.Global.java

public static String UploadFile(InetAddress ftpserverurl, String serverAppDirectory, String PureFileName,
        String fullLocFileUrl, String userName, String password) {
    // get an ftpClient object  
    FTPClient ftpClient = new FTPClient();
    FileInputStream inputStream = null;
    String responsTxt = "";
    try {/*from w  ww .ja  v  a  2  s . c  o m*/
        // pass directory path on server to connect
        // pass username and password, returned true if authentication is  
        // successful  
        ftpClient.connect(ftpserverurl, 21);
        boolean login = ftpClient.login(userName, password);
        ftpClient.setKeepAlive(false);
        ftpClient.setPassiveNatWorkaround(true);
        if (login) {
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            File firstLocalFile = new File(fullLocFileUrl);
            inputStream = new FileInputStream(firstLocalFile);
            //inputStream.reset();
            boolean uploaded = ftpClient.storeFile(serverAppDirectory + PureFileName, inputStream);
            inputStream.close();
            responsTxt = ftpClient.getReplyString();
            if (uploaded) {
                responsTxt += "File uploaded successfully !";
            } else {
                responsTxt += "Error in uploading file !::" + serverAppDirectory + PureFileName;
            }

            Global.updateLogMsg(Global.logMsgID, "\r\n\r\n\r\nUpload Response ==>\r\n" + responsTxt,
                    Global.logTbl, Global.gnrlDateStr, Global.rnUser_ID);
            // logout the user, returned true if logout successfully  
            boolean logout = ftpClient.logout();
            if (logout) {
                //System.out.println("Connection close...");
            }
        } else {
            Global.errorLog += "Connection Failed..." + responsTxt;
            Global.updateLogMsg(Global.logMsgID,
                    "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + Global.errorLog, Global.logTbl,
                    Global.gnrlDateStr, Global.rnUser_ID);
            Global.writeToLog();
        }
        return responsTxt;
    } catch (SocketException e) {
        Global.errorLog += e.getMessage() + "\r\n" + Arrays.toString(e.getStackTrace());
        Global.updateLogMsg(Global.logMsgID,
                "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + Global.errorLog, Global.logTbl,
                Global.gnrlDateStr, Global.rnUser_ID);
        Global.writeToLog();
    } catch (IOException e) {
        Global.errorLog += e.getMessage() + "\r\n" + Arrays.toString(e.getStackTrace());
        Global.updateLogMsg(Global.logMsgID,
                "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + Global.errorLog, Global.logTbl,
                Global.gnrlDateStr, Global.rnUser_ID);
        Global.writeToLog();
    } finally {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            Global.errorLog += e.getMessage() + "\r\n" + Arrays.toString(e.getStackTrace());
            Global.updateLogMsg(Global.logMsgID,
                    "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + Global.errorLog, Global.logTbl,
                    Global.gnrlDateStr, Global.rnUser_ID);
            Global.writeToLog();
        } finally {

        }
    }
    return "";
}

From source file:rems.Global.java

public static String DownloadFile(InetAddress ftpserverurl, String serverAppDirectory, String PureFileName,
        String fullLocFileUrl, String userName, String password) {

    File f = new File(fullLocFileUrl);
    // get an ftpClient object  
    FTPClient ftpClient = new FTPClient();
    String responsTxt = "";
    try {//from  www  .  ja v  a 2s  . c om
        // pass directory path on server to connect  
        ftpClient.connect(ftpserverurl, 21);
        // pass username and password, returned true if authentication is  
        // successful  
        boolean login = ftpClient.login(userName, password);
        if (login) {
            ftpClient.enterLocalPassiveMode();
            //ftpClient.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(f));
            boolean download = ftpClient.retrieveFile(serverAppDirectory + PureFileName, outputStream1);
            outputStream1.close();

            //fos = new FileOutputStream(fullLocFileUrl);
            //boolean download = ftpClient.retrieveFile(serverAppDirectory + PureFileName, fos);
            responsTxt = ftpClient.getReplyString();
            if (download) {
                responsTxt += "File downloaded successfully !";
            } else {
                responsTxt += "Error in downloading file !::" + serverAppDirectory + PureFileName;
            }
            Global.updateLogMsg(Global.logMsgID, "\r\n\r\nDownload Response ==>\r\n" + responsTxt,
                    Global.logTbl, Global.gnrlDateStr, Global.rnUser_ID);
            Global.writeToLog();
            // logout the user, returned true if logout successfully  
            boolean logout = ftpClient.logout();
            if (logout) {
                //System.out.println("Connection close...");
            }
        } else {
            Global.errorLog += "Connection Failed..." + responsTxt;
            Global.updateLogMsg(Global.logMsgID,
                    "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + Global.errorLog, Global.logTbl,
                    Global.gnrlDateStr, Global.rnUser_ID);
            Global.writeToLog();
        }
        return responsTxt;
    } catch (SocketException e) {
        Global.errorLog += Arrays.toString(e.getStackTrace());
        Global.updateLogMsg(Global.logMsgID,
                "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + Global.errorLog, Global.logTbl,
                Global.gnrlDateStr, Global.rnUser_ID);
        Global.writeToLog();
    } catch (IOException e) {
        Global.errorLog += Arrays.toString(e.getStackTrace());
        Global.updateLogMsg(Global.logMsgID,
                "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + Global.errorLog, Global.logTbl,
                Global.gnrlDateStr, Global.rnUser_ID);
        Global.writeToLog();
    } finally {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            Global.errorLog += Arrays.toString(e.getStackTrace());
            Global.updateLogMsg(Global.logMsgID,
                    "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + Global.errorLog, Global.logTbl,
                    Global.gnrlDateStr, Global.rnUser_ID);
            Global.writeToLog();
        } finally {

        }
    }
    Global.updateLogMsg(Global.logMsgID, "\r\n\r\n\r\nThe Program has Errored Out ==>\r\n\r\n" + responsTxt,
            Global.logTbl, Global.gnrlDateStr, Global.rnUser_ID);
    return responsTxt;
}

From source file:rems.Global.java

public static String[] GetFileList(InetAddress ftpServerAddrs, String serverAppDirectory, String dirName,
        String userName, String password) {
    String[] downloadFiles = new String[1];
    StringBuilder result = new StringBuilder();
    FTPClient ftpClient = new FTPClient();
    try {/*w w w  .j  a va 2s.  com*/
        // pass directory path on server to connect  
        ftpClient.connect(ftpServerAddrs);
        // pass username and password, returned true if authentication is  
        // successful  
        boolean login = ftpClient.login(userName, password);
        if (login) {
            //System.out.println("Connection established...");
            // get all files from server and store them in an array of  
            // FTPFiles  
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (file.getType() == FTPFile.FILE_TYPE) {
                    result.append(file.getName());
                    result.append("\n");
                }
            }
            result.replace(result.toString().lastIndexOf("\n"), result.toString().length(), "");
            // logout the user, returned true if logout successfully  
            boolean logout = ftpClient.logout();
            if (logout) {
                System.out.println("Connection close...");
            }
        } else {
            System.out.println("Connection fail...");
        }
        return result.toString().split("\\\n");
    } catch (SocketException e) {
        Global.errorLog += Arrays.toString(e.getStackTrace());
        Global.writeToLog();
    } catch (IOException e) {
        Global.errorLog += Arrays.toString(e.getStackTrace());
        Global.writeToLog();
    } finally {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            Global.errorLog += Arrays.toString(e.getStackTrace());
            Global.writeToLog();
        }
    }
    return downloadFiles;
}

From source file:ru.in360.FTPUploader.java

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

    try {//from   w w  w  . j  av 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 w w  w.  ja v  a 2  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:s32a.Client.Startup.FTPHandler.java

/**
 * Checks whether client was able to login with given info.
 *
 * @return//w ww  .  j ava 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;
        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 w  w w .  j a v  a2 s. co 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./* w w  w  . java 2s. 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() {
    FTPClient client = new FTPSClient(false);
    FileInputStream fis = null;//from ww w  . j  a  va2s  . co  m
    FileOutputStream fos = null;

    try {
        System.out.println("connecting");
        client.connect("athena.fhict.nl");
        boolean login = client.login("i293443", "ifvr2edfh101");
        System.out.println("login: " + login);
        client.enterLocalPassiveMode();
        System.out.println("connected: " + client.isConnected() + ", available: " + client.isAvailable());

        client.setFileType(FTP.ASCII_FILE_TYPE);
        //
        // Create an InputStream of the file to be uploaded
        //
        String filename = ".gitattributes";
        File file = new File(filename);
        file.createNewFile();
        System.out.println(file.length());
        fis = new FileInputStream(file.getAbsolutePath());
        client.makeDirectory("/Airhockey/Codebase/test");
        client.makeDirectory("\\Airhockey\\Codebase\\testey");

        //
        // Store file to server
        //
        String desiredName = "s32a\\Server\\.gitattributes";
        System.out.println("storefile: " + file.getAbsolutePath() + " - "
                + client.storeFile("/Airhockey/" + desiredName, fis));
        System.out.println("file stored");

        //            File output = new File("colors.json");
        //            fos = new FileOutputStream(output.getAbsolutePath());
        //            client.retrieveFile("/colors.json", fos);
        client.logout();
    } catch (IOException e) {
        e.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();
        }
    }

}