1 Reply Latest reply: Jan 13, 2008 5:19 PM by EJP RSS

    please needs help with dir, password, download files

    807603
      Hi the is my code if u can help me to get code.

      this is the code.

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.net.URL;

      public class myftp2 {

      static String host = null;
      static String path = "/";


      public static void main(String[] args) {


      if(args.length != 1) {
      usage();
      System.exit(1);
      }
      host = "ftp://"+args[0];

      if( ! isOpen() )
      {
      System.out.println("Cannot connect to "+host+path);
      System.exit(1);
      }
      System.out.println("Connected to "+host+path);



      BufferedReader kb = new BufferedReader( new InputStreamReader(System.in));

      boolean finished = false;
      while( ! finished ) {
      String input;
      String[] cmdArgs;

      System.out.print("myftp> ");
      try {
      input = kb.readLine();
      cmdArgs = input.split("
      s");
      if(cmdArgs[0].equals("exit") ) {
      finished = true;
      }
      else if( cmdArgs[0].equals("dir")) {
      dirCommand();
      }

      else if (cmdArgs[0].equals("get"))
      {
      //();
      }
      else if( cmdArgs[0].equals("pwd")) {
      System.out.println(host+path);
      }
      else if( cmdArgs[0].equals("cd")) {
      if(cmdArgs.length!= 2) {
      System.out.println("You must include the directory name");
      }
      else
      cdCommand(cmdArgs[1]);
      }

      else if (cmdArgs[0].equals("cd.."))
      {

      //cdCommand(cmdArgs[1]);
      }



      }


      catch(IOException e){

      }


      }
      System.out.println("Goodbye");
      }

      static void usage() {
      System.out.println("Usage: java myftp <hostname>");
      System.out.println("please write your username:password@<hostname>");

      }

      static boolean isOpen() {
      try {
      URL url = new URL(host+path);
      InputStream in = url.openStream();
      in.close();
      }
      catch(IOException e) {
      System.out.println(e.getMessage());
      return false;
      }
      return true;
      }

      static void dirCommand() {
      String line;
      try {
      URL url = new URL(host+path);

      InputStream in = url.openStream();
      InputStreamReader inStr = new InputStreamReader(in);
      BufferedReader inNet = new BufferedReader(inStr);

      while((line = inNet.readLine()) != null)
      {
      System.out.println(line);
      }
      }
      catch(IOException e) {
      System.out.println("Error: cant list the directory");
      }
      }

      static void cdCommand(String directory) {

      }

      static void cdCommandback(String dir)

      {

      }

      }