aplicacion.sistema.version.logic.JakartaFtpWrapper.java Source code

Java tutorial

Introduction

Here is the source code for aplicacion.sistema.version.logic.JakartaFtpWrapper.java

Source

package aplicacion.sistema.version.logic;

/* <!-- in case someone opens this in a browser... --> <pre> */
import org.apache.commons.net.ftp.*;

import java.util.Vector;
import java.io.*;
import java.net.UnknownHostException;

/** This is a simple wrapper around the Jakarta Commons FTP
  * library. I really just added a few convenience methods to the
  * class to suit my needs and make my code easier to read.
  * <p>
  * If you want more information on the Jakarta Commons libraries
  * (there is a LOT more you can do than what you see here), go to:
  *      http://jakarta.apache.org/commons
  * <p>
  * This Java class requires both the Jakarta Commons Net library
  * and the Jakarta ORO library (available at http://jakarta.apache.org/oro ).
  * Make sure you have both of the jar files in your path to compile.
  * Both are free to use, and both are covered under the Apache license
  * that you can read on the apache.org website. If you plan to use these
  * libraries in your applications, please refer to the Apache license first.
  * While the libraries are free, you should double-check to make sure you 
  * don't violate the license by using or distributing it (especially if you use it
  * in a commercial application).
  * <p>
  * Program version 1.0. Author Julian Robichaux, http://www.nsftools.com
  *
  * @author Julian Robichaux ( http://www.nsftools.com )
  * @version 1.0
  */

public class JakartaFtpWrapper extends FTPClient {
    private int progress = 0;

    /** A convenience method for connecting and logging in */
    public boolean connectAndLogin(String host, String userName, String password)
            throws IOException, UnknownHostException, FTPConnectionClosedException {
        boolean success = false;
        connect(host);
        int reply = getReplyCode();
        if (FTPReply.isPositiveCompletion(reply))
            success = login(userName, password);
        if (!success)
            disconnect();
        return success;
    }

    /** Turn passive transfer mode on or off. If Passive mode is active, a
      * PASV command to be issued and interpreted before data transfers;
      * otherwise, a PORT command will be used for data transfers. If you're
      * unsure which one to use, you probably want Passive mode to be on. */
    public void setPassiveMode(boolean setPassive) {
        if (setPassive)
            enterLocalPassiveMode();
        else
            enterLocalActiveMode();
    }

    /** Use ASCII mode for file transfers */
    public boolean ascii() throws IOException {
        return setFileType(FTP.ASCII_FILE_TYPE);
    }

    /** Use Binary mode for file transfers */
    public boolean binary() throws IOException {
        return setFileType(FTP.BINARY_FILE_TYPE);
    }

    /** Download a file from the server, and save it to the specified local file */
    public boolean downloadFile(String serverFile, String localFile)
            throws IOException, FTPConnectionClosedException {
        FileOutputStream out = new FileOutputStream(localFile);
        boolean result = retrieveFile(serverFile, out);

        out.close();
        return result;
    }

    /** Upload a file to the server */
    public boolean uploadFile(String localFile, String serverFile)
            throws IOException, FTPConnectionClosedException {
        FileInputStream in = new FileInputStream(localFile);
        boolean result = storeFile(serverFile, in);
        in.close();
        return result;
    }

    /** Get the list of files in the current directory as a Vector of Strings 
      * (excludes subdirectories) */
    public Vector listFileNames() throws IOException, FTPConnectionClosedException {
        FTPFile[] files = listFiles();
        Vector v = new Vector();
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isDirectory())
                v.addElement(files[i].getName());
        }
        return v;
    }

    public Vector getFiles() throws IOException, FTPConnectionClosedException {
        FTPFile[] files = listFiles();
        Vector v = new Vector();
        for (int i = 0; i < files.length; i++) {
            v.add(files[i]);
        }
        return v;
    }

    /** Get the list of files in the current directory as a single Strings,
      * delimited by \n (char '10') (excludes subdirectories) */
    public String listFileNamesString() throws IOException, FTPConnectionClosedException {
        return vectorToString(listFileNames(), "\n");
    }

    /** Get the list of subdirectories in the current directory as a Vector of Strings 
      * (excludes files) */
    public Vector listSubdirNames() throws IOException, FTPConnectionClosedException {
        FTPFile[] files = listFiles();
        Vector v = new Vector();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory())
                v.addElement(files[i].getName());
        }
        return v;
    }

    /** Get the list of subdirectories in the current directory as a single Strings,
      * delimited by \n (char '10') (excludes files) */
    public String listSubdirNamesString() throws IOException, FTPConnectionClosedException {
        return vectorToString(listSubdirNames(), "\n");
    }

    /** Convert a Vector to a delimited String */
    private String vectorToString(Vector v, String delim) {
        StringBuffer sb = new StringBuffer();
        String s = "";
        for (int i = 0; i < v.size(); i++) {
            sb.append(s).append((String) v.elementAt(i));
            s = delim;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        JakartaFtpWrapper ftp = new JakartaFtpWrapper();
        try {

            boolean connected = ftp.connectAndLogin("gisbertrepuestos.com.ar", "gisbertrepuestos.com.ar",
                    "ipsilon@1982");
            if (connected) {
                System.out.println("BISBER REPUESTOS LOGIN");
                ftp.setPassiveMode(true);
                ftp.downloadFile("/beta/beta-patch.msp", "beta-patch.msp");
                System.out.println("Downloades files ok");

            }

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FTPConnectionClosedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}