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, int port) throws SocketException, IOException 

Source Link

Document

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

Usage

From source file:ftp.search.Index.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    try {//w w w  .jav  a2  s. co  m
        // TODO add your handling code here:
        Class.forName("com.mysql.jdbc.Driver");
        String uid = "root";
        String pwd = "clever";
        String dbURL = "jdbc:mysql://localhost:3306/ftp";
        Connection conn = DriverManager.getConnection(dbURL, uid, pwd);
        Statement statement = conn.createStatement();
        FTPClient ftpClient = new FTPClient();
        String ftp = jTextField1.getText();
        int port = 21;
        try {
            ftpClient.connect(ftp, port);
            int a = ftpClient.getConnectTimeout();
            ftpClient.login("anonymous", "");
            // lists files and directories in the current working directory
            insertFiles(ftp, "/", statement, ftpClient);

            JOptionPane.showMessageDialog(this, "Files Indexed Successfully");
            ftpClient.logout();
            ftpClient.disconnect();
        } catch (IOException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        }

    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.nostra13.universalimageloader.core.download.BaseImageDownloader.java

protected InputStream getStreamFromFTPNetwork(String imageUri, Object extra) throws IOException {
    int indexUrl = imageUri.indexOf("//") + 1;
    int indexUrlFinal = imageUri.lastIndexOf(":");
    int slash = imageUri.lastIndexOf("/");
    String ip = imageUri.substring(indexUrl + 1, indexUrlFinal);
    FTPClient client = new FTPClient();
    client.connect(ip, 20000);
    client.login("anonymous", "");
    client.setFileType(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();//from w  w w . j a v a2 s  . c  om

    InputStream imageStream = null;
    FTPFile[] directories = client.listFiles();
    long fileLength = (int) getFile(directories, imageUri.substring(slash + 1)).getSize();
    try {
        imageStream = client.retrieveFileStream(imageUri.substring(slash + 1));
    } catch (IOException e) {
        // Read all data to allow reuse connection (http://bit.ly/1ad35PY)
        IoUtils.readAndCloseStream(imageStream);
        throw e;
    }
    return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), fileLength);
}

From source file:edu.stanford.epad.common.util.FTPUtil.java

public void getFile(String remoteFile, File localFile) throws Exception {

    FTPClient ftpClient = new FTPClient();
    OutputStream outputStream = null;
    try {// w  w w  .  j a  v  a 2s.  co  m
        ftpClient.connect(ftpHost, ftpPort);
        ftpClient.enterLocalPassiveMode();
        if (ftpUser != null && ftpUser.length() > 0)
            ftpClient.login(ftpUser, ftpPassword);
        else
            ftpClient.login("anonymous", "");
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
        InputStream inputStream = ftpClient.retrieveFileStream(remoteFile);
        IOUtils.copy(inputStream, outputStream);
        outputStream.flush();
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
        boolean commandOK = ftpClient.completePendingCommand();
        //boolean success = ftpClient.retrieveFile(remoteFile, outputStream);

        //if (!success) {
        //   throw new Exception("Error retrieving remote file: " + remoteFile);
        //}   
    } finally {
        outputStream.close();
        ftpClient.disconnect();
    }
}

From source file:beans.BL.java

/**
 * Upload FTP Server/*from  ww w.jav  a2  s .  c  om*/
 *
 * @param filename
 * @throws IOException
 */
public void upload(String filename) throws IOException {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;

    client.connect("ftp.sunlime.at", 990);
    client.login("admin", "secret");

    fis = new FileInputStream(filename);
    client.storeFile(filename, fis);
    client.logout();
    fis.close();
}

From source file:com.haha01haha01.harail.DatabaseDownloader.java

private Boolean downloadFile(String server, int portNumber, String user, String password, String filename,
        File localFile) throws IOException {
    FTPClient ftp = null;

    try {// w  w  w. ja v  a  2 s .co  m
        ftp = new FTPClient();
        ftp.setBufferSize(1024 * 1024);
        ftp.connect(server, portNumber);
        Log.d(NAME, "Connected. Reply: " + ftp.getReplyString());
        if (!ftp.login(user, password)) {
            return false;
        }
        Log.d(NAME, "Logged in");
        if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
            return false;
        }
        Log.d(NAME, "Downloading");
        ftp.enterLocalPassiveMode();

        OutputStream outputStream = null;
        boolean success = false;
        try {
            outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
            success = ftp.retrieveFile(filename, outputStream);
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }

        return success;
    } finally {
        if (ftp != null) {
            ftp.logout();
            ftp.disconnect();
        }
    }
}

From source file:com.eryansky.common.utils.ftp.FtpFactory.java

/**
 * ?FTP?.// ww w . ja v  a  2  s .c o m
 * 
 * @param path
 *            FTP??
 * @param filename
 *            FTP???
 * @param input
 *            ?
 * @return ?true?false
 */
public boolean ftpUploadFile(String path, String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    ftp.setControlEncoding("UTF-8");
    try {
        int reply;
        ftp.connect(url, port);
        ftp.login(username, password);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        // 
        ftp.changeWorkingDirectory(path);
        ftp.setBufferSize(1024);
        ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
        // 
        ftp.storeFile(filename, input);
        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return success;
}

From source file:net.shopxx.plugin.ftpStorage.FtpStoragePlugin.java

@Override
public void upload(String path, File file, String contentType) {
    PluginConfig pluginConfig = getPluginConfig();
    if (pluginConfig != null) {
        String host = pluginConfig.getAttribute("host");
        Integer port = Integer.valueOf(pluginConfig.getAttribute("port"));
        String username = pluginConfig.getAttribute("username");
        String password = pluginConfig.getAttribute("password");
        FTPClient ftpClient = new FTPClient();
        InputStream inputStream = null;
        try {//from ww  w . j a va  2 s .  c  om
            inputStream = new BufferedInputStream(new FileInputStream(file));
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                String directory = StringUtils.substringBeforeLast(path, "/");
                String filename = StringUtils.substringAfterLast(path, "/");
                if (!ftpClient.changeWorkingDirectory(directory)) {
                    String[] paths = StringUtils.split(directory, "/");
                    String p = "/";
                    ftpClient.changeWorkingDirectory(p);
                    for (String s : paths) {
                        p += s + "/";
                        if (!ftpClient.changeWorkingDirectory(p)) {
                            ftpClient.makeDirectory(s);
                            ftpClient.changeWorkingDirectory(p);
                        }
                    }
                }
                ftpClient.storeFile(filename, inputStream);
                ftpClient.logout();
            }
        } catch (SocketException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(inputStream);
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.disconnect();
                }
            } catch (IOException e) {
            }
        }
    }
}

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

public void uploadSingleFileWithFTP(String direcPath, FileTransferController fileTransferController) {
    FTPClient ftpClient = new FTPClient();
    InputStream inputStream = null;
    try {/*from ww  w.j  a  va2s.c om*/
        FtpProperties properties = new ResourcesProperties().loadFTPProperties();
        ftpClient.connect(properties.getFtp_server(), properties.getFtp_port());
        ftpClient.login(properties.getFtp_username(), properties.getFtp_password());
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        File secondLocalFile = new File(direcPath);
        String secondRemoteFile = "WorkshopDay2.docx";
        inputStream = new FileInputStream(secondLocalFile);
        System.out.println("Start uploading second file");
        OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
        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) {
            System.out.println("The second file is uploaded successfully.");
        }
    } 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:jenkins.plugins.publish_over_ftp.BapFtpHostConfiguration.java

private void connect(final BapFtpClient client) throws IOException {
    final FTPClient ftpClient = client.getFtpClient();
    ftpClient.connect(getHostnameTrimmed(), getPort());
    final int responseCode = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(responseCode)) {
        exception(client, Messages.exception_connectFailed(getHostnameTrimmed(), getPort(), responseCode));
    }/*from w  w w  .  j  a  v  a  2s  . c o  m*/
    setDataTransferMode(ftpClient);
}

From source file:com.buzz.buzzdata.MongoBuzz.java

private InputStream getFTPInputStream(String ftp_location) {
    InputStream retval = null;//from ww w  . ja  v  a  2 s  . co  m

    String server = "162.219.245.61";
    int port = 21;
    String user = "jelastic-ftp";
    String pass = "HeZCHxeefB";
    FTPClient ftpClient = new FTPClient();

    try {
        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
        retval = ftpClient.retrieveFileStream(ftp_location);
    } catch (IOException ex) {
        Logger.getLogger(MongoBuzz.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            Logger.getLogger(MongoBuzz.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return retval;
}