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

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

Introduction

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

Prototype

public void enterLocalPassiveMode() 

Source Link

Document

Set the current data connection mode to PASSIVE_LOCAL_DATA_CONNECTION_MODE .

Usage

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);// w  w  w.java  2 s .  c  o  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
            }
        }
    }
}

From source file:de.idos.updates.lookup.FtpLookup.java

private Version findLatestVersion() throws IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(inetAddress);/*from   www.  jav a 2 s .  c  o  m*/
    ftpClient.enterLocalPassiveMode();

    if (login != null) {
        ftpClient.login(login, null);
    }

    if (workingDir != null) {
        ftpClient.changeWorkingDirectory(workingDir);
    }

    FTPFile[] availableVersionsDirs = ftpClient.listDirectories();
    List<String> strings = new ArrayList<String>();
    for (FTPFile ftpFile : availableVersionsDirs) {
        strings.add(ftpFile.getName());
    }

    List<Version> versions = new VersionFactory().createVersionsFromStrings(strings);
    return new VersionFinder().findLatestVersion(versions);
}

From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java

/**
 * DOCUMENT ME!/* w  ww .  ja  v  a 2 s.c  o m*/
 *
 * @param   in               DOCUMENT ME!
 * @param   destinationPath  DOCUMENT ME!
 *
 * @throws  Exception  DOCUMENT ME!
 */
public void upload(final InputStream in, final String destinationPath) throws Exception {
    final FTPClient connectedFtpClient = getConnectedFTPClient();
    connectedFtpClient.enterLocalPassiveMode();
    connectedFtpClient.setFileType(BINARY_FILE_TYPE);
    connectedFtpClient.storeFile(destinationPath, in);
}

From source file:de.cismet.cids.custom.utils.formsolutions.FormSolutionFtpClient.java

/**
 * DOCUMENT ME!/*ww  w. j a va2s . co  m*/
 *
 * @param   destinationPath  DOCUMENT ME!
 * @param   out              DOCUMENT ME!
 *
 * @throws  Exception  DOCUMENT ME!
 */
public void download(final String destinationPath, final OutputStream out) throws Exception {
    final FTPClient connectedFtpClient = getConnectedFTPClient();
    connectedFtpClient.enterLocalPassiveMode();
    connectedFtpClient.setFileType(BINARY_FILE_TYPE);
    if (!connectedFtpClient.retrieveFile(destinationPath, out)) {
        throw new Exception("file " + destinationPath + " not found");
    }
}

From source file:conf.FTPConf.java

public String subirArchivo(File archivo) {

    FTPClient ftpClient = new FTPClient();
    try {//from   w  w w.ja va2  s.  com

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #2: uploads second file using an OutputStream
        String serverFile = dir + archivo.getName();
        InputStream inputStream = new FileInputStream(archivo);

        OutputStream outputStream = ftpClient.storeFileStream(serverFile);
        byte[] bytesIn = new byte[4096];
        int read = 0;

        while ((read = inputStream.read(bytesIn)) != -1) {
            outputStream.write(bytesIn, 0, read);
        }
        inputStream.close();
        outputStream.close();

        boolean completed = ftpClient.completePendingCommand();
        if (completed) {
            String link = "ftp://" + user + "@" + server + "/" + serverFile;
            return link;
        }

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return null;
}

From source file:de.jwi.ftp.FTPUploader.java

public static String upload(URL url, List files) {
    String rcs = "";

    if (!"ftp".equals(url.getProtocol())) {
        return "not ftp protocol";
    }//from   ww w  . j  av a2 s  .c o  m

    String host = url.getHost();
    String userInfo = url.getUserInfo();
    String path = url.getPath();
    String user = null;
    String pass = null;

    int p;
    if ((userInfo != null) && ((p = userInfo.indexOf(':')) > -1)) {
        user = userInfo.substring(0, p);
        pass = userInfo.substring(p + 1);
    } else {
        user = userInfo;
    }

    FTPClient ftp = new FTPClient();

    try {
        int reply;
        ftp.connect(host);

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

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return "connection refused";
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        return "could not connect to " + host;
    }

    try {
        if (!ftp.login(user, pass)) {
            ftp.logout();
            return "failed to login";
        }

        ftp.setFileType(FTP.BINARY_FILE_TYPE);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        ftp.enterLocalPassiveMode();

        rcs = uploadFiles(ftp, path, files);

        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        return "connection closed";
    } catch (IOException e) {
        return e.getMessage();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    return rcs;
}

From source file:boosta.artem.services.FTPResultReader.java

public void readResult() {
    String server = "62.210.82.210";
    int port = 55021;
    String user = "misha_p";
    String pass = "GfhjkM1983";

    FTPClient ftpClient = new FTPClient();
    try {/*from ww w  .j  a  va2s .  c o m*/

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.ASCII_FILE_TYPE);

        // APPROACH #1: using retrieveFile(String, OutputStream)
        String remoteFile = Main.remote_out_path;
        System.out.println(remoteFile);
        //String remoteFile = "/results/ttgenerate.txt";
        String downlFile = Main.file_name;
        System.out.println(downlFile);
        File downloadFile = new File(downlFile);
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));

        boolean success = ftpClient.retrieveFile(remoteFile, outputStream);

        outputStream.close();

        if (success) {
            System.out.println(" ?   FTP!!!");
        } else {
            System.out.println("  ?   TFP!!!");
        }

    } catch (IOException ex) {
        System.out.println("   ? FTP  !!!");
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            System.out.println(" ? ?? ? FTP!!!");
        }
    }
}

From source file:ca.nrc.cadc.web.ServerToServerFTPTransfer.java

public void send(final OutputStreamWrapper outputStreamWrapper, final String filename) throws IOException {
    final FTPClient ftpClient = connect();
    ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();

    try {//  ww  w  .  j  a  v a  2 s  .  c  o m
        final OutputStream outputStream = ftpClient.storeFileStream(filename);

        outputStreamWrapper.write(outputStream);

        outputStream.flush();
        outputStream.close();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }
}

From source file:heigit.ors.routing.traffic.providers.FtpDataSource.java

@Override
public String getMessage() throws IOException {
    String message = null;//from w  w w.  j av a  2  s.c  om
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(server, port);
        ftpClient.login(user, password);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(2);

        String remoteFile1 = "/" + file;
        InputStream inputStream = ftpClient.retrieveFileStream(remoteFile1);
        message = StreamUtility.readStream(inputStream, "iso-8859-1");
        Boolean success = ftpClient.completePendingCommand();

        if (success) {
            System.out.println("File has been downloaded successfully.");
        }

        inputStream.close();
    } catch (IOException e) {
        /* SendMail mail = new SendMail();
           try
           {
             mail.postMail(this.properties.getProperty("mail"), "TMC-Fehler", 
        e.getStackTrace().toString(), "TMC@Fehlermeldung.de", this.properties
        .getProperty("smtpHost"), this.properties
        .getProperty("smtpUser"), this.properties
        .getProperty("smtpPort"));
           }
           catch (MessagingException e1)
           {
             e1.printStackTrace();
           }
           this.logger.debug("Error with FTP connection " + 
             e.getLocalizedMessage(), e);
           throw new DownloadException(
             "Error while downloading file from FTP " + 
             e.getLocalizedMessage(), e);
         */
    }

    return message;
}

From source file:model.ProfilModel.java

public void uploadFoto(String path) {
    String server = "localhost";
    int port = 21;
    String user = "user1";
    String pass = "itsme";

    FTPClient ftpClient = new FTPClient();
    try {//from  w w  w  .j a v  a2s. c  om

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #1: uploads first file using an InputStream
        File firstLocalFile = new File(path);

        String firstRemoteFile = this.user.getFoto();
        InputStream inputStream = new FileInputStream(firstLocalFile);

        System.out.println("Start uploading first file");
        boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
        if (done) {
            System.out.println("The first file is uploaded successfully.");
        }
        inputStream.close();

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}