Example usage for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE

List of usage examples for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE

Introduction

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

Prototype

int BINARY_FILE_TYPE

To view the source code for org.apache.commons.net.ftp FTP BINARY_FILE_TYPE.

Click Source Link

Document

A constant used to indicate the file(s) being transfered should be treated as a binary image, i.e., no translations should be performed.

Usage

From source file:FTPConnect.FTPSubirArchivo.java

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;/*from www.j a  v  a 2 s  . c om*/
    boolean result;
    String ftpServerAddress = "localhost";
    String userName = "admin";
    String password = "admin";

    try {
        ftpclient.connect(ftpServerAddress);
        result = ftpclient.login(userName, password);

        if (result == true) {
            System.out.println("Logged in Successfully !");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/");

        File file = new File("D:/File.doc");
        String testName = file.getName();
        fis = new FileInputStream(file);

        // Upload file to the ftp server
        result = ftpclient.storeFile(testName, fis);

        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        ftpclient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpclient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }
}

From source file:ftp_server.FTPFileUpload.java

public static void main(String[] args) throws IOException {
    FTPClient ftpclient = new FTPClient();
    FileInputStream fis = null;//from w  w  w.  j  a v  a 2 s  .c  o m
    boolean result;
    String ftpServerAddress = "shamalmahesh.net78.net";
    String userName = "a9959679";
    String password = "9P3IckDo";

    try {
        ftpclient.connect(ftpServerAddress);
        result = ftpclient.login(userName, password);

        if (result == true) {
            System.out.println("Logged in Successfully !");
        } else {
            System.out.println("Login Fail!");
            return;
        }
        ftpclient.enterLocalPassiveMode();
        ftpclient.setFileType(FTP.BINARY_FILE_TYPE);

        ftpclient.changeWorkingDirectory("/public_html/testing");

        File file = new File("D:/jpg/wq.jpg");
        String testName = file.getName();
        fis = new FileInputStream(file);

        // Upload file to the ftp server
        result = ftpclient.storeFile(testName, fis);

        if (result == true) {
            System.out.println("File is uploaded successfully");
        } else {
            System.out.println("File uploading failed");
        }
        ftpclient.logout();
    } catch (FTPConnectionClosedException e) {
        e.printStackTrace();
    } finally {
        try {
            ftpclient.disconnect();
        } catch (FTPConnectionClosedException e) {
            System.out.println(e);
        }
    }
}

From source file:com.run.FtpClientExample.java

public static void main(String[] args) {

    FTPClient client = new FTPClient();
    FileOutputStream fos = null;//from  w  w w.j  ava 2  s .  co m

    try {
        client.connect(ARDroneConstants.IP_ADDRESS, ARDroneConstants.FTP_PORT);

        if (!client.login("anonymous", ""))
            System.err.println("login failed");

        client.setFileType(FTP.BINARY_FILE_TYPE);

        String filename = "version.txt";
        fos = new FileOutputStream(filename);

        if (!client.retrieveFile("/" + filename, fos))
            System.err.println("cannot find file");

        System.out.println("done");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.flush();
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:airnowgrib2tojson.AirNowGRIB2toJSON.java

/**
 * @param args the command line arguments
 *///w  ww.  j  a  va 2s .  c o m
public static void main(String[] args) {

    SimpleDateFormat GMT = new SimpleDateFormat("yyMMddHH");
    GMT.setTimeZone(TimeZone.getTimeZone("GMT-2"));

    System.out.println(GMT.format(new Date()));

    FTPClient ftpClient = new FTPClient();
    FileOutputStream fos = null;

    try {
        //Connecting to AirNow FTP server to get the fresh AQI data  
        ftpClient.connect("ftp.airnowapi.org");
        ftpClient.login("pixelshade", "GZDN8uqduwvk");
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        //downloading .grib2 file
        File of = new File("US-" + GMT.format(new Date()) + "_combined.grib2");
        OutputStream outstr = new BufferedOutputStream(new FileOutputStream(of));
        InputStream instr = ftpClient
                .retrieveFileStream("GRIB2/US-" + GMT.format(new Date()) + "_combined.grib2");
        byte[] bytesArray = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = instr.read(bytesArray)) != -1) {
            outstr.write(bytesArray, 0, bytesRead);
        }

        //Close used resources
        ftpClient.completePendingCommand();
        outstr.close();
        instr.close();

        // logout the user 
        ftpClient.logout();

    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            //disconnect from AirNow server
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        //Open .grib2 file
        final File AQIfile = new File("US-" + GMT.format(new Date()) + "_combined.grib2");
        final GridDataset gridDS = GridDataset.open(AQIfile.getAbsolutePath());

        //The data type needed - AQI; since it isn't defined in GRIB2 standard,
        //Aerosol type is used instead; look AirNow API documentation for details.
        GridDatatype AQI = gridDS.findGridDatatype("Aerosol_type_msl");

        //Get the coordinate system for selected data type;
        //cut the rectangle to work with - time and height axes aren't present in these files
        //and latitude/longitude go "-1", which means all the data provided.
        GridCoordSystem AQIGCS = AQI.getCoordinateSystem();
        List<CoordinateAxis> AQI_XY = AQIGCS.getCoordinateAxes();
        Array AQIslice = AQI.readDataSlice(0, 0, -1, -1);

        //Variables for iterating through coordinates
        VariableDS var = AQI.getVariable();
        Index index = AQIslice.getIndex();

        //Variables for counting lat/long from the indices provided
        double stepX = (AQI_XY.get(2).getMaxValue() - AQI_XY.get(2).getMinValue()) / index.getShape(1);
        double stepY = (AQI_XY.get(1).getMaxValue() - AQI_XY.get(1).getMinValue()) / index.getShape(0);
        double curX = AQI_XY.get(2).getMinValue();
        double curY = AQI_XY.get(1).getMinValue();

        //Output details
        OutputStream ValLog = new FileOutputStream("USA_AQI.json");
        Writer ValWriter = new OutputStreamWriter(ValLog);

        for (int j = 0; j < index.getShape(0); j++) {
            for (int i = 0; i < index.getShape(1); i++) {
                float val = AQIslice.getFloat(index.set(j, i));

                //Write the AQI value and its coordinates if it's present by i/j indices
                if (!Float.isNaN(val))
                    ValWriter.write("{\r\n\"lat\":" + curX + ",\r\n\"lng\":" + curY + ",\r\n\"AQI\":" + val
                            + ",\r\n},\r\n");

                curX += stepX;
            }
            curY += stepY;
            curX = AQI_XY.get(2).getMinValue();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.logic.test.FTPSLogic.java

public static void main(String[] args) {
    String serverAdress = "62.2.176.167";
    String username = "RLSFTPRead";
    String password = "ftp4rls";
    int port = 990;
    FTPSClient ftpsClient = new FTPSClient("TLS", true);
    String remoteFile = "REM - Persons Extract.csv";
    File localFile = new File("Persons Extract.csv");

    try {/*from   w  w w  .  jav a 2  s .co  m*/
        TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };

        ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

        //ftpsClient.setTrustManager(trustManager[0]);
        //KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        //kmf.init(null, null);
        //KeyManager km = kmf.getKeyManagers()[0];

        //ftpsClient.setKeyManager(km);
        ftpsClient.setBufferSize(1024 * 1024);
        ftpsClient.setConnectTimeout(100000);
        ftpsClient.connect(InetAddress.getByName(serverAdress), port);
        ftpsClient.setSoTimeout(100000);

        if (ftpsClient.login(username, password)) {
            ftpsClient.execPBSZ(0);
            ftpsClient.execPROT("P");
            ftpsClient.changeWorkingDirectory("/");
            ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpsClient.enterLocalPassiveMode();

            //ftpsClient.retrieveFile(remoteFile, new FileOutputStream(localFile));
            for (FTPFile file : ftpsClient.listFiles()) {
                System.out.println("Nom " + file.getName());
            }

        }
    } catch (SocketException e) {
        ;
    } catch (UnknownHostException e) {
        ;
    } catch (IOException e) {
        ;
    } catch (Exception e) {
        ;
    } finally {
        try {
            ftpsClient.logout();
        } catch (Exception e2) {
        }

        try {
            ftpsClient.disconnect();
        } catch (Exception e2) {
        }
    }
}

From source file:examples.ftp.FTPExample.java

public static final void main(String[] args) {
    int base = 0;
    boolean storeFile = false, binaryTransfer = false, error = false;
    String server, username, password, remote, local;
    FTPClient ftp;//from  w  w w. ja va  2 s  . c  o m

    for (base = 0; base < args.length; base++) {
        if (args[base].startsWith("-s"))
            storeFile = true;
        else if (args[base].startsWith("-b"))
            binaryTransfer = true;
        else
            break;
    }

    if ((args.length - base) != 5) {
        System.err.println(USAGE);
        System.exit(1);
    }

    server = args[base++];
    username = args[base++];
    password = args[base++];
    remote = args[base++];
    local = args[base];

    ftp = new FTPClient();
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

    try {
        int reply;
        ftp.connect(server);
        System.out.println("Connected to " + server + ".");

        // After connection attempt, you should check the reply code to verify
        // success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        System.exit(1);
    }

    __main: try {
        if (!ftp.login(username, password)) {
            ftp.logout();
            error = true;
            break __main;
        }

        System.out.println("Remote system is " + ftp.getSystemName());

        if (binaryTransfer)
            ftp.setFileType(FTP.BINARY_FILE_TYPE);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        ftp.enterLocalPassiveMode();

        if (storeFile) {
            InputStream input;

            input = new FileInputStream(local);

            ftp.storeFile(remote, input);

            input.close();
        } else {
            OutputStream output;

            output = new FileOutputStream(local);

            ftp.retrieveFile(remote, output);

            output.close();
        }

        ftp.logout();
    } catch (FTPConnectionClosedException e) {
        error = true;
        System.err.println("Server closed connection.");
        e.printStackTrace();
    } catch (IOException e) {
        error = true;
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    System.exit(error ? 1 : 0);
}

From source file:examples.ftp.FTPSExample.java

public static final void main(String[] args) throws NoSuchAlgorithmException {
    int base = 0;
    boolean storeFile = false, binaryTransfer = false, error = false;
    String server, username, password, remote, local;
    String protocol = "SSL"; // SSL/TLS
    FTPSClient ftps;//w  w w. j  ava 2 s  .  c o  m

    for (base = 0; base < args.length; base++) {
        if (args[base].startsWith("-s"))
            storeFile = true;
        else if (args[base].startsWith("-b"))
            binaryTransfer = true;
        else
            break;
    }

    if ((args.length - base) != 5) {
        System.err.println(USAGE);
        System.exit(1);
    }

    server = args[base++];
    username = args[base++];
    password = args[base++];
    remote = args[base++];
    local = args[base];

    ftps = new FTPSClient(protocol);

    ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

    try {
        int reply;

        ftps.connect(server);
        System.out.println("Connected to " + server + ".");

        // After connection attempt, you should check the reply code to verify
        // success.
        reply = ftps.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftps.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }
    } catch (IOException e) {
        if (ftps.isConnected()) {
            try {
                ftps.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        System.exit(1);
    }

    __main: try {
        ftps.setBufferSize(1000);

        if (!ftps.login(username, password)) {
            ftps.logout();
            error = true;
            break __main;
        }

        System.out.println("Remote system is " + ftps.getSystemName());

        if (binaryTransfer)
            ftps.setFileType(FTP.BINARY_FILE_TYPE);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        ftps.enterLocalPassiveMode();

        if (storeFile) {
            InputStream input;

            input = new FileInputStream(local);

            ftps.storeFile(remote, input);

            input.close();
        } else {
            OutputStream output;

            output = new FileOutputStream(local);

            ftps.retrieveFile(remote, output);

            output.close();
        }

        ftps.logout();
    } catch (FTPConnectionClosedException e) {
        error = true;
        System.err.println("Server closed connection.");
        e.printStackTrace();
    } catch (IOException e) {
        error = true;
        e.printStackTrace();
    } finally {
        if (ftps.isConnected()) {
            try {
                ftps.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }

    System.exit(error ? 1 : 0);
}

From source file:com.incosyz.sms.other.UploadBackup.java

public static void uploadBackup(String filePath, String fileName) {
    String server = "ftp.bambooblindslanka.com";
    int port = 21;
    String user = "samagi@bambooblindslanka.com";
    String pass = "fs82xKzHZvtU";

    FTPClient client = new FTPClient();
    try {/*  w  ww  .  ja  va2  s  .com*/
        client.connect(server, port);
        client.login(user, pass);
        client.enterLocalPassiveMode();
        client.setFileType(FTP.BINARY_FILE_TYPE);
        File firstLocalFile = new File(filePath);

        String firstRemoteFile = fileName;
        InputStream inputStream = new FileInputStream(firstLocalFile);

        boolean done = client.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
    } catch (IOException ex) {
        Logger.getLogger(UploadBackup.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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  2s . 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("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:deincraftlauncher.IO.download.FTPConnection.java

public static void connect() {

    if (connected) {
        return;//  www .j  a va 2s  .  c om
    }

    try {
        client.connect(FTPSync.getFtpServer());
        client.enterLocalPassiveMode();
        client.login(FTPSync.getFtpUsername(), FTPSync.getFtpPassword());
        client.setFileType(FTP.BINARY_FILE_TYPE);

    } catch (IOException ex) {
        Logger.getLogger(FTPConnection.class.getName()).log(Level.SEVERE, null, ex);
    }

    connected = true;
}