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:uk.sipperfly.utils.FTPUtil.java

/**
 * Upload a single file to the FTP server.
 *
 * @param ftpClient      an instance of org.apache.commons.net.ftp.FTPClient class.
 * @param localFilePath  Path of the file on local computer
 * @param remoteFilePath Path of the file on remote the server
 * @return true if the file was uploaded successfully, false otherwise
 * @throws IOException if any network or IO error occurred.
 *//*from  w  w  w . j  a v a2  s.  com*/
public static boolean uploadSingleFile(FTPClient ftpClient, String localFilePath, String remoteFilePath)
        throws FileNotFoundException, IOException {
    File localFile = new File(localFilePath);
    try (InputStream inputStream = new FileInputStream(localFile)) {
        try {
            return ftpClient.storeFile(remoteFilePath, inputStream);
        } catch (SocketTimeoutException e) {
            Logger.getLogger(GACOM).log(Level.SEVERE, "upload Single File: ", e.getCause());
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
                Logger.getLogger(GACOM).log(Level.SEVERE, "upload Single File: ", e.getCause());
            }
            return false;
        }
    }
}

From source file:view.GenerujTest.java

private void sendfile() {

    FTPClient client = new FTPClient();
    FileInputStream fis = null;/*from  w  ww.j  av a 2s  .com*/

    try {
        client.connect("ftp.serwer1749827.home.pl");
        client.login("serwer", "serwer123456");

        //
        // Create an InputStream of the file to be uploaded
        //
        client.removeDirectory("index.html");
        String filename = "index.html";
        fis = new FileInputStream(filename);

        //
        // Store file to server
        //
        client.storeFile(filename, fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:websync2.SyncDesign.java

public void copyFileFTP(File temp, File servF) {

    InputStream inputStream = null;
    try {/*  ww w.ja v  a2s.  c om*/
        FTPClient ftpclient = ftp.getFtpClient();
        ftpclient.changeToParentDirectory();
        String firstRemoteFile = servF.getAbsolutePath();
        String[] pathA = firstRemoteFile.split("/");
        if (pathA != null) {
            for (int i = 0; i < pathA.length - 1; i++) {
                if ("".equals(pathA[i])) {
                    continue;
                }
                InputStream is = ftpclient.retrieveFileStream(pathA[i]);
                int retCode = ftpclient.getReplyCode();
                if (retCode == 550) {
                    ftpclient.makeDirectory(pathA[i]);
                }
                ftpclient.changeWorkingDirectory(pathA[i]);
            }
            inputStream = new FileInputStream(temp);
            boolean done = ftpclient.storeFile(pathA[pathA.length - 1], inputStream);
            if (done) {
                System.out.println("The first file is uploaded successfully.");
            }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            inputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(SyncDesign.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}