A Simple Java FTP Client Source Code

Home arrow Java Tutorials arrow Source code arrow A Simple Java FTP Client Source Code
A Simple Java FTP Client Source Code Print E-mail
Contributed by Joe   
Tuesday, 08 August 2006
This a pure Java Ftp client source code that allow you to download and upload files from a remote computer running an FTP server

 

import java.io.*;
import java.net.*;
import java.util.*;
/**
* A class that acts as a requester to an FTP server.
* Intended as a simple illustration of the protocol.
* Does not implement any error checking and is not
* optimized for performance.
*/
public class FTPClient
{
   private String host;
   private Socket control;
   private BufferedReader cmdin;
   private PrintWriter cmdout;
   private boolean verbose;
   /**
   * Creates a new FTPClient for the specified host
   * @param host the host name
   */
   public FTPClient(String host)
   {
      this.host = host;
   }
   /**
   * Opens the control socket
   */
   public void open() throws IOException
   {
      if (control == null) {
         control = new Socket(host, 21);
         cmdin =
            new BufferedReader(
            new InputStreamReader(
            control.getInputStream()));
         cmdout =
            new PrintWriter(
            new OutputStreamWriter(
            control.getOutputStream()));
         if (verbose) {
            System.out.println("S: " + receive());
            System.out.flush();
         }
      }
   }
   /**
   * Closes the control socket
   */
   public void close() throws IOException
   {
      if (control != null) {
         cmdout.flush();
         cmdout.close();
         cmdin.close();
         control.close();
         control = null;
      }
   }
   /**
   * Logs in with the specified user ID
   * and password
   */
   public void login(String user, String pass)
      throws IOException
   {
      if (control == null)
         throw new IOException
         ("Control socket never opened");
      send("USER " + user);
      send("PASS " + pass);
   }
   /**
   * Sends a command to the server
   */
   public void send(String cmd) throws IOException
   {
      if (verbose) {
         System.out.println("C: " + cmd);
         System.out.flush();
      }
      cmdout.println(cmd);
      cmdout.flush();
      String response = receive();
      if (response.startsWith("5"))
         throw new IOException(response);
      if (verbose) {
         System.out.println("S: " + response);
         System.out.flush();
      }
   }
   /**
   * Receives a server response
   */
   public String receive() throws IOException
   {
      return cmdin.readLine();
   }
   /**
   * Changes to the specified directory
   */
   public void chdir(String dirName) throws IOException
   {
      send("CWD " + dirName);
   }
   /**
   * Receives a file and returns its contents
   * as a string.
   * @param fileName the file to be retrieved
   */
   public String getFileData(String fileName)
      throws IOException
   {
      if (verbose) {
         System.out.println
         ("C: Entering getFileData for " + fileName);
         System.out.flush();
      }
      // Change to the appropriate directory
      fileName = fileName.replace('\\', '/');
      int p = fileName.lastIndexOf("/");
      if (p >= 0) {
         String dirName = fileName.substring(0, p);
         if (verbose) {
            System.out.println
            ("C: Changing directory to " + dirName);
            System.out.flush();
         }
         chdir(dirName);
         fileName = fileName.substring(p+1);
         if (verbose) {
            System.out.println
            ("C: base file name is " + fileName);
            System.out.flush();
         }
      }
      // Open a server socket on any available port
      ServerSocket server = new ServerSocket(0);
      // Get the local host address and the port
      // number of the server socket we just opened.
      String addr =
         InetAddress.getLocalHost().getHostAddress();
      addr = addr.replace('.', ',');
      int port = server.getLocalPort();
      int portHi = port / 256;
      int portLo = port % 256;
      addr += "," + portHi + "," + portLo;
      // Send the addresses with a PORT command.
      send("PORT " + addr);
      // Now send the RETR command
      send("RETR " + fileName);
      // Wait for the FTP server to contact us on our
      // server port.
      Socket client = server.accept();
      // We are done with the server port
      server.close();
      // Open the client socket and read its data
      StringBuffer buffer = new StringBuffer();
      BufferedInputStream in =
         new BufferedInputStream(client.getInputStream());
      for (;;) {
         int c = in.read();
         if (c == -1)
            break;
         buffer.append((char) c);
      }
      in.close();
      // Return the file data
      return buffer.toString();
   }
   /**
   * Sets the verbose flag
   */
   public void setVerbose(boolean yesno)
   {
      verbose = yesno;
   }
}


  home              contact us

 

©2006-2012 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground