FTP Server and Client in Java

Submitted by Karthikeyan on Oct 30, 2012 - 07:11

Simple FTP server and Client Console Application in Java
Compile and run server and client in the separate window.
FTP Client prompt 3 options, 1. Send 2. Receive 3. Exit.
Select Send option and provide full path of the file to be sent. For e.g. D:\test.txt
Select Receive option and provide full path of the file to be received. For e.g. D:\test.txt
Use this program in LAN

FTP Server program


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class FTPServer {
public static void main(String args[]) throws Exception
    {
    ServerSocket soc = new ServerSocket(5217);
    System.out.println("FT server started on port number 5217");
    while(true)
    {
        System.out.println("Waiting for connection..");
        transferfile t = new transferfile(soc.accept());
    }
}
}
class transferfile extends Thread
{
    Socket ClientSoc;
    DataInputStream din;
    DataOutputStream dout;
    transferfile(Socket soc)
    {
        try
        {
            ClientSoc = soc;
            din = new DataInputStream(ClientSoc.getInputStream());
            dout = new DataOutputStream(ClientSoc.getOutputStream());
            System.out.println("FTP Client connected..");
            start();

        }
        catch(Exception ex)
        {

        }
    }
    void SendFile() throws Exception
    {
        String filename = din.readUTF();
        File f = new File(filename);
        if(!f.exists())
        {
            dout.writeUTF("File not found");
            return;
        }
 else
        {
            dout.writeUTF("Ready");
            FileInputStream fin = new FileInputStream(f);
            int ch;
            do
            {
                ch = fin.read();
                dout.writeUTF(String.valueOf(ch));
            }
            while(ch!=-1);
            fin.close();
            dout.writeUTF("File Received Successfully");

        }
    }
        void ReceiveFile() throws Exception
         
        {
            String filename = din.readUTF();
            String option;
            option="N";
            if(filename.compareTo("File not found")==0)
            {
                return;
            }
            File f = new File(filename);
            
            if (f.exists())
            {
                dout.writeUTF("File Already Exists");
            }
             else
            {
                dout.writeUTF("SendFile");
                option="Y";
             }
            if(option.compareTo("Y")==0)
                {
                    FileOutputStream fout=new FileOutputStream(f);
                    int ch;
                    String temp;
                    do
                    {
                    temp=din.readUTF();
                    ch=Integer.parseInt(temp);
                    if(ch!=-1)
                    {
                    fout.write(ch);
                    }
                    }
                    while(ch!=-1);
                    fout.close();
                    dout.writeUTF("File send Successfully");
                    }
                else
                    {
                    return;
                    }
          }
public void run()
{
    while(true)
    {
        try
        {
            System.out.println("Waiting for Command ...");
            String Command=din.readUTF();
            if(Command.compareTo("GET")==0)
            {
            System.out.println("\tGET Command Received...");
            SendFile();
            continue;
            }
            else if(Command.compareTo("SEND")==0)
            {
            System.out.println("\tSEND Command Received ...");
            ReceiveFile();
            continue;
            }
            else if(Command.compareTo("DISCONNECT")==0)
            {
            System.out.println("\tDisconnect command Received ...");
            System.exit(1);
            }
            }
         catch(Exception ex)
         {
         }
    }
}
}

FTP Client program


import java .io.*;
import java.net.Socket;

 class FTPClient {

    public static void main(String arg[]) throws Exception
    {
        Socket soc=new Socket("192.168.1.2" ,5217);
        transferfileClient t=new  transferfileClient(soc);
        t.displayMenu();

    }
}
class transferfileClient

{

    Socket ClientSoc;
    DataInputStream din;
    DataOutputStream dout;
    BufferedReader br;
    transferfileClient(Socket soc)
    {
        try
        {
            ClientSoc=soc;
            din=new DataInputStream(ClientSoc.getInputStream());
            dout=new DataOutputStream(ClientSoc.getOutputStream());
            br=new BufferedReader(new InputStreamReader(System.in));

        }
        catch(Exception ex)
    {
        }
    }
    void SendFile() throws Exception
            {
        String filename;
        System.out.print("Enter File Name:");
        filename=br.readLine();
        File f=new File(filename);
        if(!f.exists())
          {
            System.out.println("file not Exists...");
            dout.writeUTF("File not found");
            return;
          }
        dout.writeUTF(filename);
        String msgFromServer=din.readUTF();
        if(msgFromServer.compareTo("File Already Exists")==0)
        {
        String Option;
        System.out.println("file Already Exists.want to Overrite(y/n)?");
        Option=br.readLine();
        if(Option=="y")
        {
            dout.writeUTF("Y");

        }
 else{dout.writeUTF("N");
 return;
 }
    }
    System.out.print("Sending file...");
    FileInputStream fin=new FileInputStream(f);
    int ch;
    do
    {
        ch=fin.read();
        dout.writeUTF(String.valueOf(ch));

    }
    while(ch!=-1);
    fin.close();
    System.out.println(din.readUTF());


    }
void ReceiveFile() throws Exception
    {
        String fileName;
        System.out.print("Enter the name :");
        fileName=br.readLine();
        dout.writeUTF(fileName);
        String msgFromServer=din.readUTF();
        if(msgFromServer.compareTo("File not Found")==0)
           {
            System.out.println("File not found on Server ...");
            return;
            }
        else if(msgFromServer.compareTo("READY")==0)
            {
            System.out.println("Receiving File ...");
            File f=new File(fileName);
            if(f.exists())
            {
                      String Option;
System.out.println("File Already Exists. Want to Overwrite(Y/N) ? ");
                    Option= br.readLine();
                 if(Option=="N")
             {
                dout.flush();
                return;
            }
         }
       FileOutputStream fout=new FileOutputStream(f);
       int ch;
       String temp;
       do
       {
              temp=din.readUTF();
              ch=Integer.parseInt(temp);
              if(ch!=-1)
             {
               fout.write(ch);
             }
        }
               while(ch!=-1);
               fout.close();
               System.out.println(din.readUTF());
        }
        }
public void displayMenu() throws Exception
{
while(true)
{
System.out.println("[Menu ]");
System.out.println("1. Send File");
System.out.println("2. Receive File");
System.out.println("3.Exit");
System.out.println("\n Enter Choice :");
int choice;
choice=Integer.parseInt(br.readLine());
if(choice==1)
{
dout.writeUTF("SEND");
SendFile();
}
else if(choice==2)
{
dout.writeUTF("GET");
ReceiveFile();
}
else if(choice==3)
{
dout.writeUTF("DISCONNECT");
System.exit(1);
}
}
}
}