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

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

Introduction

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

Prototype

public boolean storeFile(String remote, InputStream local) throws IOException 

Source Link

Document

Stores a file on the server using the given name and taking input from the given InputStream.

Usage

From source file:com.clothcat.hpoolauto.HtmlUploader.java

/**
 * Upload the html via FTP/*  ww  w . j  a va 2  s  . c o m*/
 */
public static void uploadHtml() {
    FTPClient ftpc = new FTPClient();
    FTPClientConfig conf = new FTPClientConfig();
    ftpc.configure(conf);
    try {
        ftpc.connect(Constants.FTP_HOSTNAME);
        ftpc.login(Constants.FTP_USERNAME, Constants.FTP_PASSWORD);
        File dir = new File(Constants.HTML_FILEPATH);
        File[] files = dir.listFiles();
        ftpc.changeWorkingDirectory("/htdocs");
        for (File f : files) {
            HLogger.log(Level.FINEST, "Uploading file: " + f.getName());
            FileInputStream fis = new FileInputStream(f);
            ftpc.storeFile(f.getName(), fis);
        }
        ftpc.logout();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (ftpc.isConnected()) {
            try {
                ftpc.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:gui.TesteHackathon.java

public static void enviaImagem() {
    String server = "www.ejec.co";
    int port = 21;
    String user = "ejec";
    String pass = "cPanel2015";

    FTPClient ftpClient = new FTPClient();
    try {/* ww w  .  j  a  v a 2 s.com*/

        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("image.jpg");
        String firstRemoteFile = "public_html/virtualfit/image.jpg";
        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.");
        }

    } 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:cn.zhuqi.mavenssh.web.util.test.FtpTest.java

/**
 * Description: ?FTP?/*from   w ww  .ja va2s .  co m*/
 *
 * @param url FTP?hostname
 * @param port FTP???-1
 * @param username FTP?
 * @param password FTP?
 * @param path FTP??
 * @param filename FTP???
 * @param input ?
 * @return ?true?false
 */
public static boolean uploadFile(String url, int port, String username, String password, String path,
        String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        // FTP?
        if (port > -1) {
            ftp.connect(url, port);
        } else {
            ftp.connect(url);
        }
        // FTP
        ftp.login(username, password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(path);
        ftp.storeFile(filename, input);

        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        success = false;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
            }
        }
    }
    return success;
}

From source file:com.jason.thrift.TransmitHandler.java

/**
 * Description: ?FTP?/*from   w  w w.  j av  a  2s  . c  om*/
 * 
 * @Version1.0 Jul 27, 2008 4:31:09 PM by ?cuihongbao@d-heaven.com
 * @param url
 *            FTP?hostname
 * @param port
 *            FTP??
 * @param username
 *            FTP?
 * @param password
 *            FTP?
 * @param path
 *            FTP??
 * @param filename
 *            FTP???
 * @param input
 *            ?
 * @return ?true?false
 */
public static boolean uploadFile(String url, int port, String username, String password, String path,
        String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
        int reply;
        ftp.connect(url, port);// FTP?
        // ??ftp.connect(url)?FTP?
        ftp.login(username, password);// 
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return success;
        }
        ftp.changeWorkingDirectory(path);
        ftp.storeFile(filename, input);

        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
    return success;
}

From source file:mysynopsis.FTPUploader.java

public static String uploadWizard() throws IOException {

    FTPClient connect;
    connect = null;/*from  ww  w  .  j av a 2 s. co  m*/

    try {
        connect = new FTPClient();
        connect.connect(server, 21);
        int reply = connect.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            connect.disconnect();
            return "Can't Connect to the Server";
        }

        boolean check = connect.login(user, pass);
        if (!check) {
            return "Username or password Incorrect";
        }

        connect.setFileType(FTP.BINARY_FILE_TYPE);
        connect.enterLocalPassiveMode();

        InputStream input;
        try {
            input = new FileInputStream(new File("index.html"));
            connect.storeFile(dir + "index.html", input);
            input.close();
            connect.logout();
            connect.disconnect();

        } catch (IOException ex) {
            return "You need to put a slash (/) at the end";
        }
    } catch (IOException iOException) {
        return "Wrong Server Information. Please Try again";
    }
    return "File Transfer Successful!";
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

private static void uploadJdkDownloadScript(FTPClient ftp, String jdkDownloadUrl) throws IOException {

    String aspxPageName = aspScriptName;

    byte[] aspxPageData = generateAspxScriptForCustomJdkDownload(jdkDownloadUrl);
    ftp.storeFile(ftpRootPath + aspxPageName, new ByteArrayInputStream(aspxPageData));

    byte[] webConfigData = generateWebConfigForCustomJdkDownload(aspxPageName, null);
    ftp.storeFile(ftpRootPath + webConfigFilename, new ByteArrayInputStream(webConfigData));
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static void uploadWebConfig(WebApp webApp, InputStream fileStream, IProgressIndicator indicator)
        throws IOException {
    FTPClient ftp = null;
    try {//from w  ww .j a v  a  2 s .co  m
        PublishingProfile pp = webApp.getPublishingProfile();
        ftp = getFtpConnection(pp);

        if (indicator != null)
            indicator.setText("Stopping the service...");
        webApp.stop();

        if (indicator != null)
            indicator.setText("Uploading " + webConfigFilename + "...");
        ftp.storeFile(ftpRootPath + webConfigFilename, fileStream);

        if (indicator != null)
            indicator.setText("Starting the service...");
        webApp.start();
    } finally {
        if (ftp != null && ftp.isConnected()) {
            ftp.disconnect();
        }
    }
}

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

/**
 * DOCUMENT ME!//from   w ww  . ja  v a2 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:com.mendhak.gpslogger.senders.ftp.Ftp.java

public static boolean Upload(String server, String username, String password, int port, boolean useFtps,
        String protocol, boolean implicit, InputStream inputStream, String fileName) {
    FTPClient client = null;

    try {//  ww w . j av  a 2  s  .  com
        if (useFtps) {
            client = new FTPSClient(protocol, implicit);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(null, null);
            KeyManager km = kmf.getKeyManagers()[0];
            ((FTPSClient) client).setKeyManager(km);
        } else {
            client = new FTPClient();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        Utilities.LogDebug("Connecting to FTP");
        client.connect(server, port);

        Utilities.LogDebug("Logging in to FTP server");
        if (client.login(username, password)) {
            client.enterLocalPassiveMode();

            Utilities.LogDebug("Uploading file to FTP server");

            FTPFile[] existingDirectory = client.listFiles("GPSLogger");

            if (existingDirectory.length <= 0) {
                client.makeDirectory("GPSLogger");
            }

            client.changeWorkingDirectory("GPSLogger");
            boolean result = client.storeFile(fileName, inputStream);
            inputStream.close();
            if (result) {
                Utilities.LogDebug("Successfully FTPd file");
            } else {
                Utilities.LogDebug("Failed to FTP file");
            }

        } else {
            Utilities.LogDebug("Could not log in to FTP server");
            return false;
        }

    } catch (Exception e) {
        Utilities.LogError("Could not connect or upload to FTP server.", e);
    } finally {
        try {
            Utilities.LogDebug("Logging out of FTP server");
            client.logout();

            Utilities.LogDebug("Disconnecting from FTP server");
            client.disconnect();
        } catch (Exception e) {
            Utilities.LogError("Could not logout or disconnect", e);
        }
    }

    return true;
}

From source file:com.microsoft.azuretools.utils.WebAppUtils.java

public static void deployArtifact(String artifactName, String artifactPath, PublishingProfile pp,
        boolean toRoot, IProgressIndicator indicator) throws IOException {
    FTPClient ftp = null;
    InputStream input = null;//ww  w. ja  v  a2  s.c  o  m
    try {
        if (indicator != null)
            indicator.setText("Connecting to FTP server...");

        ftp = getFtpConnection(pp);

        if (indicator != null)
            indicator.setText("Uploading the application...");
        input = new FileInputStream(artifactPath);
        if (toRoot) {
            WebAppUtils.removeFtpDirectory(ftp, ftpWebAppsPath + "ROOT", indicator);
            ftp.deleteFile(ftpWebAppsPath + "ROOT.war");
            ftp.storeFile(ftpWebAppsPath + "ROOT.war", input);
        } else {
            WebAppUtils.removeFtpDirectory(ftp, ftpWebAppsPath + artifactName, indicator);
            ftp.deleteFile(artifactName + ".war");
            boolean success = ftp.storeFile(ftpWebAppsPath + artifactName + ".war", input);
            if (!success) {
                int rc = ftp.getReplyCode();
                throw new IOException("FTP client can't store the artifact, reply code: " + rc);
            }
        }
        if (indicator != null)
            indicator.setText("Logging out of FTP server...");
        ftp.logout();
    } finally {
        if (input != null)
            input.close();
        if (ftp != null && ftp.isConnected()) {
            ftp.disconnect();
        }
    }
}