File Transfer Protocol [FTP] Implementation in Java
The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files from one host to another host over a TCP-based network, such as the Internet.
FTP is built on a client-server architecture and uses separate control and data connections between the client and the server. FTP users may authenticate themselves using a clear-text sign-in protocol, normally in the form of a username and password, but can connect anonymously if the server is configured to allow it. For secure transmission that protects the username and password, and encrypts the content, FTP is often secured with SSL/TLS (FTPS). SSH File Transfer Protocol (SFTP) is sometimes also used instead, but is technologically different.
The first FTP client applications were command-line applications developed before operating systems had graphical user interfaces, and are still shipped with most Windows, Unix, and Linux operating systems. Many FTP clients and automation utilities have since been developed for desktops, servers, mobile devices, and hardware, and FTP has been incorporated into productivity applications, such as Web page editors.
Java FTP Client:
FTPC.java
import java.io.*;
import java.net.*;
class FTPC
{
public static void main(String[] args) throws Exception
{
String option;
DataInputStream in=new DataInputStream(System.in);
Socket s=new Socket("localhost",Integer.parseInt(args[0]));
System.out.println("MENU");
System.out.println("1.SEND");
System.out.println("2.RECEIVE");
FTPC ftp=new FTPC();
while(true)
{
option=in.readLine();
if(option.equals("1")){
System.out.println("SEND Command Received..");
ftp.sendfile(s);
}
else if(option.equals("2")){
System.out.println("RECEIVE Command Received..");
ftp.receivefile(s);
}
}
}
public void sendfile(Socket s) throws Exception
{
Socket ssock=s;
DataInputStream in=new DataInputStream(System.in);
DataInputStream cin=new DataInputStream(ssock.getInputStream());
DataOutputStream cout=new DataOutputStream(ssock.getOutputStream());
cout.writeUTF("RECEIVE");
String filename=in.readLine();
System.out.println("Reading File "+filename);
cout.writeUTF(filename);
File f=new File(filename);
FileInputStream fin=new FileInputStream(f);
int ch;
do
{
ch=fin.read();
cout.writeUTF(String.valueOf(ch));
}while(ch!=-1);
fin.close();
System.out.println("File Sent");
}
public void receivefile(Socket s) throws Exception
{
Socket ssock=s;
DataInputStream in=new DataInputStream(System.in);
DataInputStream cin=new DataInputStream(ssock.getInputStream());
DataOutputStream cout=new DataOutputStream(ssock.getOutputStream());
cout.writeUTF("SEND");
String filename=in.readLine();
cout.writeUTF(filename);
System.out.println("Receiving File "+filename);
File f=new File(filename);
FileOutputStream fout=new FileOutputStream(f);
int ch;
do
{
ch=Integer.parseInt(cin.readUTF());
if(ch!=-1) fout.write(ch);
}while(ch!=-1);
System.out.println("Received File...");
fout.close();
}
}
Java Server:
FTPS.java
import java.io.*;
import java.net.*;
class FTPS
{
public static void main(String[] args) throws Exception
{
ServerSocket Sock=new ServerSocket(Integer.parseInt(args[0]));
Socket s=Sock.accept();
DataInputStream cin=new DataInputStream(s.getInputStream());
DataOutputStream cout=new DataOutputStream(s.getOutputStream());
FTPS ftp=new FTPS();
while(true)
{
String option=cin.readUTF();
if(option.equals("SEND")){
System.out.println("SEND Command Received..");
ftp.sendfile(s);
}
else if(option.equals("RECEIVE")){
System.out.println("RECEIVE Command Received..");
ftp.receivefile(s);
}
}
}
public void sendfile(Socket s) throws Exception
{
Socket ssock=s;
DataInputStream cin=new DataInputStream(ssock.getInputStream());
DataOutputStream cout=new DataOutputStream(ssock.getOutputStream());
String filename=cin.readUTF();
System.out.println("Reading File "+filename);
File f=new File(filename);
FileInputStream fin=new FileInputStream(f);
int ch;
do
{
ch=fin.read();
cout.writeUTF(Integer.toString(ch));
}while(ch!=-1);
fin.close();
System.out.println("File Sent");
}
public void receivefile(Socket s) throws Exception
{
Socket ssock=s;
DataInputStream cin=new DataInputStream(ssock.getInputStream());
DataOutputStream cout=new DataOutputStream(ssock.getOutputStream());
String filename=cin.readUTF();
System.out.println("Receiving File "+filename);
File f=new File(filename);
FileOutputStream fout=new FileOutputStream(f);
int ch;
while((ch=Integer.parseInt(cin.readUTF()))!=-1)
{
fout.write(ch);
}
System.out.println("Received File...");
fout.close();
}
}
Put the two programs in different folder and use two instances of terminal to run it,start the server first
ie: java FTPS.java 9898
9898 is the port number change it to anything you like >1024
java FTPC.java 9898
kindly tell me how i can run this program in eclips.
how to execute this programm in ubuntu OS