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

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

Introduction

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

Prototype

public void connect(InetAddress host) throws SocketException, IOException 

Source Link

Document

Opens a Socket connected to a remote host at the current default port and originating from the current host at a system assigned port.

Usage

From source file:com.jaeksoft.searchlib.crawler.file.process.fileInstances.FtpsFileInstance.java

@Override
protected FTPClient ftpConnect() throws SocketException, IOException, NoSuchAlgorithmException {
    FilePathItem fpi = getFilePathItem();
    FTPClient f = new FTPSClient();
    f.connect(fpi.getHost());
    f.login(fpi.getUsername(), fpi.getPassword());
    return f;/* ww  w. j a v  a2 s  . c  om*/
}

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

private Version findLatestVersion() throws IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(inetAddress);
    ftpClient.enterLocalPassiveMode();//from  w  w  w  . ja va  2 s.  com

    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:com.discursive.jccook.net.FTPExample.java

public void start() throws IOException {

    FTPClient client = new FTPClient();
    OutputStream outStream = null;

    try {/*from w  w w.j a va2s.c om*/
        client.connect("ftp.ibiblio.org");
        client.login("anonymous", "");

        String remoteFile = "/pub/micro/commodore/schematics/computers/c64/c64bus.gif";
        outStream = new FileOutputStream("c64bus.gif");

        client.retrieveFile(remoteFile, outStream);
    } catch (IOException ioe) {
        System.out.println("Error communicating with FTP server.");
    } finally {
        IOUtils.closeQuietly(outStream);
        try {
            client.disconnect();
        } catch (IOException e) {
            System.out.println("Problem disconnecting from FTP server");
        }
    }

    secondExample();

}

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

/**
 * DOCUMENT ME!//from w  w  w.  jav a 2 s .  co  m
 *
 * @return  DOCUMENT ME!
 *
 * @throws  Exception  DOCUMENT ME!
 */
private FTPClient getConnectedFTPClient() throws Exception {
    final FTPClient ftpClient = new FTPClient();
    ftpClient.connect(FormSolutionsProperties.getInstance().getFtpHost());

    final int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftpClient.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftpClient.login(FormSolutionsProperties.getInstance().getFtpLogin(),
            FormSolutionsProperties.getInstance().getFtpPass());
    return ftpClient;
}

From source file:ftp_server.FileUpload.java

public void uploadingWithProgress() {
    FTPClient ftp = new FTPClient();
    FileInputStream fis = null;//from  w  ww  .  j  a  v  a 2s  .  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();
    }
}

From source file:com.claim.support.FtpUtil.java

public void uploadMutiFileWithFTP(String targetDirectory, FileTransferController fileTransferController) {
    try {//  w  ww  .  ja  v a 2 s  .  co m
        FtpProperties properties = new ResourcesProperties().loadFTPProperties();

        FTPClient ftp = new FTPClient();
        ftp.connect(properties.getFtp_server());
        if (!ftp.login(properties.getFtp_username(), properties.getFtp_password())) {
            ftp.logout();
        }
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        ftp.enterLocalPassiveMode();
        System.out.println("Remote system is " + ftp.getSystemName());
        ftp.changeWorkingDirectory(targetDirectory);
        System.out.println("Current directory is " + ftp.printWorkingDirectory());
        FTPFile[] ftpFiles = ftp.listFiles();
        if (ftpFiles != null && ftpFiles.length > 0) {
            for (FTPFile file : ftpFiles) {
                if (!file.isFile()) {
                    continue;
                }
                System.out.println("File is " + file.getName());
                OutputStream output;
                output = new FileOutputStream(
                        properties.getFtp_remote_directory() + File.separator + file.getName());
                ftp.retrieveFile(file.getName(), output);
                output.close();
            }
        }
        ftp.logout();
        ftp.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:it.zero11.acme.example.FTPChallengeListener.java

private boolean createChallengeFiles(String token, String challengeBody) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {/*from w  w w .  j  a v  a  2s.com*/
        ftp.connect(host);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return false;
        }

        ftp.login(username, password);
        ftp.changeWorkingDirectory(webroot);
        ftp.makeDirectory(".well-known");
        ftp.changeWorkingDirectory(".well-known");
        ftp.makeDirectory("acme-challenge");
        ftp.changeWorkingDirectory("acme-challenge");
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE, FTPClient.BINARY_FILE_TYPE);
        ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        success = ftp.storeFile(token, new ByteArrayInputStream(challengeBody.getBytes()));
        if (!success)
            System.err.println("FTP error uploading file: " + ftp.getReplyCode() + ": " + ftp.getReplyString());
        ftp.logout();
    } catch (IOException e) {
        throw new AcmeException(e);
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }

    return success;
}

From source file:it.zero11.acme.example.FTPChallengeListener.java

private void deleteChallengeFiles() {
    FTPClient ftp = new FTPClient();
    try {/*w  ww  . j av a2s .  co m*/
        ftp.connect(host);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return;
        }

        ftp.login(username, password);
        ftp.changeWorkingDirectory(webroot);
        ftp.changeWorkingDirectory(".well-known");
        ftp.changeWorkingDirectory("acme-challenge");

        FTPFile[] subFiles = ftp.listFiles();

        if (subFiles != null && subFiles.length > 0) {
            for (FTPFile aFile : subFiles) {
                String currentFileName = aFile.getName();
                if (currentFileName.equals(".") || currentFileName.equals("..")) {
                    continue;
                } else {
                    ftp.deleteFile(currentFileName);
                }
            }
        }
        ftp.changeToParentDirectory();
        ftp.removeDirectory("acme-challenge");
        ftp.changeToParentDirectory();
        ftp.removeDirectory(".well-known");
        ftp.logout();
    } catch (IOException e) {
        throw new AcmeException(e);
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:ftp_server.FileUpload.java

public void uploading() {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;/*from  www.  ja v a 2s .c  o  m*/
    try {
        client.connect("shamalmahesh.net78.net");
        client.login("a9959679", "9P3IckDo");
        client.setFileType(FTP.BINARY_FILE_TYPE);
        int reply = client.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Uploading....");
        } else {
            System.out.println("Failed connect to the server!");
        }
        //            String filename = "DownloadedLook1.txt";
        String path = "D:\\jpg\\";
        //            String filename = "1.jpg";
        String filename = "appearance.jpg";
        System.out.println(path + filename);
        fis = new FileInputStream(path + filename);
        System.out.println(fis.toString());
        boolean status = client.storeFile("/public_html/testing/" + filename, fis);
        System.out.println("Status : " + status);
        System.out.println("Done!");
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.starr.smartbuilds.service.FileService.java

public String getFile(Build build, ServletContext sc)
        throws FileNotFoundException, IOException, ParseException {

    /*if (fileName == null || fileName.equals("") || fileText == null || fileText.equals("")) {
     resultMsg = "<font color='red'>Fail: File is not created!</font>";
     } else {//  ww  w  . j a v a  2 s . c om
     try {*/
    String path = sc.getRealPath("/");
    System.out.println(path);
    File dir = new File(path + "/builds");
    if (!dir.exists() || !dir.isDirectory()) {
        dir.mkdir();
    }

    String fileName = path + "/builds/" + build.getChampion().getKeyChamp() + build.getId() + ".json";
    File fileBuild = new File(fileName);
    if (!fileBuild.exists() || fileBuild.isDirectory()) {
        String file_data = buildService.buildData(build, buildService.parseBlocks(build.getBlocks()));
        fileBuild.createNewFile();
        FileOutputStream fos = new FileOutputStream(fileBuild, false);
        fos.write(file_data.getBytes());
        fos.close();
    }

    FTPClient client = new FTPClient();
    FileInputStream fis = null;

    try {
        client.connect("itelit.ftp.ukraine.com.ua");
        client.login("itelit_dor", "154896");

        // Create an InputStream of the file to be uploaded
        fis = new FileInputStream(fileBuild.getAbsolutePath());

        // Store file to server
        client.storeFile("builds/" + build.getChampion().getKeyChamp() + build.getId() + ".json", fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*} catch (IOException ex) {
     resultMsg = "<font color='red'>Fail: File is not created!</font>";
     }
     }*/
    return "http://dor.it-elit.org/builds/" + build.getChampion().getKeyChamp() + build.getId() + ".json";
}