Package com.acelet.lib

Source Code of com.acelet.lib.Ftp

/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */

package com.acelet.lib;

import java.io.*;
import java.util.*;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ProtocolCommandEvent;
import org.apache.commons.net.ProtocolCommandListener;

public class Ftp implements FtpConstants {
  String host = "";
  String command = "";
  String remoteFile = "";
  String localFile = "";
  String fileType = "";
  String userName = "";
  String password = "";

  FTPClient ftpClient;

  public Ftp(String host, String command, String remoteFile, String localFile,
  String fileType, String userName, String password) {
    this.host = host;
    this.command = command;
    this.remoteFile = remoteFile;
    this.localFile = localFile;
    this.fileType = fileType;
    this.userName = userName;
    this.password = password;
   
    ftpClient = new FTPClient();
    if (Externals.isDebugging)
      ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
  }

  public int ftp() throws Exception {
    InputStream inputStream = null;
    OutputStream outputStream = null;

   
   
   
    try {
      ftpClient.connect(host);
      int reply = ftpClient.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply))
        throw new Exception(ftpClient.getReplyString());

      if (!ftpClient.login(userName, password))
        throw new Exception(ftpClient.getReplyString());

      if (fileType.equalsIgnoreCase(TYPE_BINARY))
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

     
      ftpClient.enterLocalPassiveMode();

      if (command.equalsIgnoreCase(COMMAND_GET)) {
        outputStream = new FileOutputStream(localFile);
        if (ftpClient.retrieveFile(remoteFile, outputStream) == false) {
          throw new Exception(ftpClient.getReplyString());
        }
      } else {
        inputStream = new FileInputStream(localFile);
        if (ftpClient.storeFile(remoteFile, inputStream) == false) {
          throw new Exception(ftpClient.getReplyString());
        }
      }
    } finally {
      if (ftpClient != null) {
        try {
          ftpClient.logout();
        } catch (Exception e1) {
          ;
        }
        try {
          ftpClient.disconnect();
        } catch (Exception e2) {
          ;
        }
      }
      if (inputStream != null)
        inputStream.close();
      if (outputStream != null)
        outputStream.close();
    }
    return 0;
  }

  class PrintCommandListener implements ProtocolCommandListener
  {
    private PrintWriter __writer;
   
    public PrintCommandListener(PrintWriter writer)
    {
      __writer = writer;
    }
   
    public void protocolCommandSent(ProtocolCommandEvent event)
    {
      __writer.print(event.getMessage());
      __writer.flush();
    }
   
    public void protocolReplyReceived(ProtocolCommandEvent event)
    {
      __writer.print(event.getMessage());
      __writer.flush();
    }
  }

  public static void main(String[] args) throws Exception {
    String host = args[0];
    String command = args[1];
    String remoteFile = args[2];
    String localFile = args[3];
    String fileType = args[4];
    String userName = args[5];
    String password = args[6];

    /*
    System.out.println(
                       "host="+host+
                       " command="+command+
                       " remoteFile="+remoteFile+
                       " localFile="+localFile+
                       " fileType="+fileType+
                       " userName="+userName+
                       " password="+password);
    */

    Ftp ftp = new Ftp(host, command, remoteFile, localFile, fileType, userName, password);
    int exitCode = ftp.ftp();
    System.exit(exitCode);
  }
}


TOP

Related Classes of com.acelet.lib.Ftp

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.