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

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

Introduction

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

Prototype

public boolean login(String username, String password) throws IOException 

Source Link

Document

Login to the FTP server using the provided username and password.

Usage

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   www  . j  ava 2  s . 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:heigit.ors.routing.traffic.providers.FtpDataSource.java

@Override
public String getMessage() throws IOException {
    String message = null;/*w w w. j av  a  2  s. co m*/
    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:br.com.tiagods.util.FTPDownload.java

public boolean downloadFile(String arquivo) {
    FTPConfig cf = FTPConfig.getInstance();
    FTPClient ftp = new FTPClient();
    try {/*from  w w  w .  j  av a  2  s . c o m*/
        ftp.connect(cf.getValue("host"), Integer.parseInt(cf.getValue("port")));
        ftp.login(cf.getValue("user"), cf.getValue("pass"));
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        String remoteFile1 = cf.getValue("dirFTP") + "/" + arquivo;
        novoArquivo = new File(System.getProperty("java.io.tmpdir") + "/" + arquivo);
        try (OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(novoArquivo))) {
            boolean success = ftp.retrieveFile(remoteFile1, outputStream1);
            if (success) {
                return true;
            }
        }
        return false;
    } catch (IOException e) {
        System.out.println("Erro = " + e.getMessage());
        return false;
    } finally {
        try {
            if (ftp.isConnected()) {
                ftp.logout();
                ftp.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:br.com.tiagods.util.FTPUpload.java

public boolean uploadFile(File arquivo, String novoNome) {
    FTPConfig cf = FTPConfig.getInstance();

    FTPClient ftp = new FTPClient();
    try {//from  ww  w .  j  a  va  2 s .  co m
        ftp.connect(cf.getValue("host"), Integer.parseInt(cf.getValue("port")));
        ftp.login(cf.getValue("user"), cf.getValue("pass"));
        //ftp.connect(server,port);
        //ftp.login(user,pass);
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        boolean done;
        try (InputStream stream = new FileInputStream(arquivo)) {
            String remoteFile = novoNome;
            System.out.println("Start uploading first file");
            done = ftp.storeFile(cf.getValue("dirFTP") + "/" + remoteFile, stream);
            stream.close();
        }
        if (done) {
            JOptionPane.showMessageDialog(null, "Arquivo enviado com sucesso!");
            return true;
        }
        return false;
    } catch (IOException e) {
        System.out.println("Erro = " + e.getMessage());
        return false;
    } finally {
        try {
            if (ftp.isConnected()) {
                ftp.logout();
                ftp.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

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

/**
 *
 * @param ftpClient     The Connected client.
 * @return              The mutated client, now authenticated.
 * @throws AccessControlException    If login fails.
 *//*from w w  w .java 2 s  .co  m*/
private FTPClient login(final FTPClient ftpClient) throws AccessControlException, IOException {
    if (!ftpClient.login(CREDENTIAL, CREDENTIAL)) {
        ftpClient.logout();
        throw new AccessControlException(String.format("Login failed for %s", CREDENTIAL));
    } else {
        return ftpClient;
    }
}

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 a2  s .c o  m*/

        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();
        }
    }
}

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

private Version findLatestVersion() throws IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(inetAddress);/*from w w  w . j  av  a  2s.  com*/
    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:br.com.atmatech.painel.util.EnviaLogs.java

public void enviarBD(File file, String enderecoFTP, String login, String senha) throws IOException {
    String nomeArquivo = null;/*  ww  w.  ja  v  a  2s . c  o  m*/
    FTPClient ftp = new FTPClient();
    ftp.connect(enderecoFTP);
    //verifica se conectou com sucesso! 

    if (FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
        ftp.login(login, senha);
    } else {
        //erro ao se conectar  
        ftp.disconnect();
    }
    ftp.changeWorkingDirectory("/atmatech.com.br/ErroLogsPainelFX/");
    //abre um stream com o arquivo a ser enviado              
    InputStream is = new FileInputStream(file);
    //pega apenas o nome do arquivo             
    int idx = file.getName().lastIndexOf(File.separator);
    if (idx < 0)
        idx = 0;
    else
        idx++;
    nomeArquivo = file.getName().substring(idx, file.getName().length());
    //ajusta o tipo do arquivo a ser enviado  
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    //faz o envio do arquivo  
    ftp.storeFile(nomeArquivo, is);
    ftp.disconnect();

}

From source file:co.nubetech.hiho.mapreduce.lib.output.FTPTextOutputFormat.java

@Override
public RecordWriter<K, V> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {

    Configuration conf = job.getConfiguration();

    String ip = conf.get(HIHOConf.FTP_ADDRESS);
    String portno = conf.get(HIHOConf.FTP_PORT);
    String usr = conf.get(HIHOConf.FTP_USER);
    String pwd = conf.get(HIHOConf.FTP_PASSWORD);
    String dir = getOutputPath(job).toString();
    System.out.println("\n\ninside ftpoutputformat" + ip + " " + portno + " " + usr + " " + pwd + " " + dir);
    String keyValueSeparator = conf.get("mapred.textoutputformat.separator", "\t");
    FTPClient f = new FTPClient();
    f.connect(ip, Integer.parseInt(portno));
    f.login(usr, pwd);
    f.changeWorkingDirectory(dir);//from  ww w  . j  a  va  2 s .co m
    f.setFileType(FTP.BINARY_FILE_TYPE);

    boolean isCompressed = getCompressOutput(job);
    CompressionCodec codec = null;
    String extension = "";
    if (isCompressed) {
        Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job, GzipCodec.class);
        codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
        extension = codec.getDefaultExtension();
    }
    Path file = getDefaultWorkFile(job, extension);
    FileSystem fs = file.getFileSystem(conf);
    String filename = file.getName();
    if (!isCompressed) {
        // FSDataOutputStream fileOut = fs.create(file, false);
        OutputStream os = f.appendFileStream(filename);
        DataOutputStream fileOut = new DataOutputStream(os);
        return new FTPLineRecordWriter<K, V>(fileOut, new String(keyValueSeparator), f);

    } else {
        // FSDataOutputStream fileOut = fs.create(file, false);
        OutputStream os = f.appendFileStream(filename);
        DataOutputStream fileOut = new DataOutputStream(os);
        return new FTPLineRecordWriter<K, V>(new DataOutputStream(codec.createOutputStream(fileOut)),
                keyValueSeparator, f);
    }
}

From source file:ftp_server.FileUpload.java

public void uploadingWithProgress() {
    FTPClient ftp = new FTPClient();
    FileInputStream fis = null;/*  w  ww.jav a2s  .c  o m*/
    try {
        ftp.connect("shamalmahesh.net78.net");
        ftp.login("a9959679", "9P3IckDo");
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.changeWorkingDirectory("/public_html/testing");
        int reply = ftp.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Uploading....");
        } else {
            System.out.println("Failed connect to the server!");
        }
        File f1 = new File("D:\\jpg\\1.jpg");

        //            fis = new FileInputStream(ftp.storeFile("one.jpg", fis));
        System.out.println("Done!");
    } catch (IOException e) {
        e.printStackTrace();
    }
}