Example usage for org.apache.commons.net.ftp FTPConnectionClosedException printStackTrace

List of usage examples for org.apache.commons.net.ftp FTPConnectionClosedException printStackTrace

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:FTPConnect.FTPConnect.java

public static void main(String[] args) throws IOException {
    FTPClient ftpClient = new FTPClient();
    boolean result;
    try {/*from  www. jav  a2 s . c om*/
        // Connect to the localhost
        ftpClient.connect("localhost");

        // login to ftp server
        result = ftpClient.login("admin", "password");
        if (result == true) {
            System.out.println("Successfully logged in!");
        } else {
            System.out.println("Login Fail!");
            return;
        }
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpClient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }

}

From source file:FTPConnect.FTPBajarArchivo.java

public static void main(String[] args) throws IOException {
    FTPClient ftpClient = new FTPClient();
    FileOutputStream fos = null;/*from  ww w . ja v a  2 s .c  om*/
    boolean result;
    try {
        // Connect to the localhost
        ftpClient.connect("localhost");

        // login to ftp server
        result = ftpClient.login("", "");
        if (result == true) {
            System.out.println("Successfully logged in!");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        String fileName = "uploadfile.txt";
        fos = new FileOutputStream(fileName);

        // Download file from the ftp server
        result = ftpClient.retrieveFile(fileName, fos);

        if (result == true) {
            System.out.println("File downloaded successfully !");
        } else {
            System.out.println("File downloading failed !");
        }
        ftpClient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            ftpClient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.err.println(e);
        }
    }
}

From source file:FTPConnect.FTPSubirArchivo.java

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;/*from   w ww  .j a  v  a 2  s.c om*/
    boolean result;
    String ftpServerAddress = "localhost";
    String userName = "admin";
    String password = "admin";

    try {
        ftpclient.connect(ftpServerAddress);
        result = ftpclient.login(userName, password);

        if (result == true) {
            System.out.println("Logged in Successfully !");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/");

        File file = new File("D:/File.doc");
        String testName = file.getName();
        fis = new FileInputStream(file);

        // Upload file to the ftp server
        result = ftpclient.storeFile(testName, fis);

        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        ftpclient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpclient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }
}

From source file:ftp_server.FTPFileUpload.java

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;//www. j ava  2  s .  c om
    boolean result;
    String ftpServerAddress = "shamalmahesh.net78.net";
    String userName = "a9959679";
    String password = "9P3IckDo";

    try {
        ftpclient.connect(ftpServerAddress);
        result = ftpclient.login(userName, password);

        if (result == true) {
            System.out.println("Logged in Successfully !");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        ftpclient.enterLocalPassiveMode();
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/public_html/testing");

        File file = new File("D:/jpg/wq.jpg");
        String testName = file.getName();
        fis = new FileInputStream(file);

        // Upload file to the ftp server
        result = ftpclient.storeFile(testName, fis);

        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        ftpclient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpclient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }
}

From source file:FTP.Uploader.java

public void uploadFile(String filename) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;/*w ww  .  j ava2 s  .  c o  m*/
    boolean result;
    String ftpServerAddress = "shamalmahesh.net78.net";
    String userName = "a9959679";
    String password = "9P3IckDo";

    try {
        ftpclient.connect(ftpServerAddress);
        result = ftpclient.login(userName, password);

        if (result == true) {
            System.out.println("Logged in Successfully !");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        ftpclient.enterLocalPassiveMode();
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/public_html/testing");

        //            File file = new File("D:/jpg/wq.jpg");
        File file = new File(filename);
        String testName = file.getName();
        fis = new FileInputStream(file);
        System.out.println(ftpclient.getBufferSize());
        // Upload file to the ftp server
        result = ftpclient.storeFile(testName, fis);
        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        ftpclient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpclient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }
}

From source file:com.gdbocom.util.SendFileinFTP.java

/**
 * ,?FTP?BINASCII?/* w  ww . j  a va2s .  c  om*/
 * @param local ??(?,"/"?),??
 * @param remotePath ()
 * @param remote ??(??),??
 * @param filetype ?,BINASCII??
 * @param ftptype FTP?,??
 * @return ??
 */
public boolean getFile(String local, String remotePath, String remote, int filetype, int ftptype)
        throws IOException {

    //bin?,ASCII?
    if (filetype == SendFileinFTP.BINARY_FILE_TYPE) {
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        gzLog.Write("BIN?");
    } else if (filetype == SendFileinFTP.ASCII_FILE_TYPE) {
        ftp.setFileType(FTP.ASCII_FILE_TYPE);
        gzLog.Write("ASCII?");
    } else {
        gzLog.Write("");
        return false;
    }

    // FTP??,FTP?
    if (ftptype == SendFileinFTP.ActiveMode) {
        ftp.enterLocalActiveMode();
        gzLog.Write("FTP?");
    } else if (ftptype == SendFileinFTP.PassiveMode) {
        ftp.enterLocalPassiveMode();
        gzLog.Write("FTP?");
    } else {
        ftp.enterLocalPassiveMode();
        gzLog.Write("FTP?,FTP?");
        return false;
    }

    //?
    ftp.changeWorkingDirectory(remotePath);

    //
    try {
        OutputStream output;
        output = new FileOutputStream(local);

        if (ftp.retrieveFile(remote, output)) {
            gzLog.Write("" + remote + "?,?:./" + local);
        } else {
            gzLog.Write("");
        }
        output.close();

    } catch (FTPConnectionClosedException e) {
        gzLog.Write("Server closed connection.");
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:edu.cmu.cs.in.hoop.hoops.load.HoopFTPReader.java

/**
 * /* w w  w  .  j  a  va 2s . c om*/
 */
private String retrieveFTP(String aURL) {
    debug("retrieveFTP (" + aURL + ")");

    URL urlObject = null;

    try {
        urlObject = new URL(aURL);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String downloadPath = this.projectToFullPath("<PROJECTPATH>/tmp/download/");
    HoopLink.fManager.createDirectory(downloadPath);

    File translator = new File(urlObject.getFile());

    String localFileName = "<PROJECTPATH>/tmp/download/" + translator.getName();

    OutputStream fileStream = null;

    if (HoopLink.fManager.openStreamBinary(this.projectToFullPath(localFileName)) == false) {
        this.setErrorString("Error opening temporary output file");
        return (null);
    }

    fileStream = HoopLink.fManager.getOutputStreamBinary();

    if (fileStream == null) {
        this.setErrorString("Error opening temporary output file");
        return (null);
    }

    debug("Starting FTP client ...");

    FTPClient ftp = new FTPClient();

    try {
        int reply;

        debug("Connecting ...");

        ftp.connect(urlObject.getHost());

        debug("Connected to " + urlObject.getHost() + ".");
        debug(ftp.getReplyString());

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

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            debug("FTP server refused connection.");
            return (null);
        } else {
            ftp.login("anonymous", "hoop-dev@gmail.com");

            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                debug("Unable to login to FTP server");
                return (null);
            }

            debug("Logged in");

            boolean rep = true;

            String pathFixed = translator.getParent().replace("\\", "/");

            rep = ftp.changeWorkingDirectory(pathFixed);
            if (rep == false) {
                debug("Unable to change working directory to: " + pathFixed);
                return (null);
            } else {
                debug("Current working directory: " + pathFixed);

                debug("Retrieving file " + urlObject.getFile() + " ...");

                try {
                    rep = ftp.retrieveFile(urlObject.getFile(), fileStream);
                } catch (FTPConnectionClosedException connEx) {
                    debug("Caught: FTPConnectionClosedException");
                    connEx.printStackTrace();
                    return (null);
                } catch (CopyStreamException strEx) {
                    debug("Caught: CopyStreamException");
                    strEx.printStackTrace();
                    return (null);
                } catch (IOException ioEx) {
                    debug("Caught: IOException");
                    ioEx.printStackTrace();
                    return (null);
                }

                debug("File retrieved");
            }

            ftp.logout();
        }
    } catch (IOException e) {
        debug("Error retrieving FTP file");
        e.printStackTrace();
        return (null);
    } finally {
        if (ftp.isConnected()) {
            debug("Closing ftp connection ...");

            try {
                ftp.disconnect();
            } catch (IOException ioe) {
                debug("Exception closing ftp connection");
            }
        }
    }

    debug("Closing local file stream ...");

    try {
        fileStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String result = HoopLink.fManager.loadContents(this.projectToFullPath(localFileName));

    return (result);
}