How to transfer or copy a file between computers using java RMI

This is a simple Java project using RMI to show you the basics of how to transfer a file from one computer to another. The application has two sections. 1) Server and 2)Client.

1) Server Section.

1 Open Eclipse and create a new Java Project via : File –> New –> Java Project

copy-file-rmi1

2 Set the name of the project as : MyFileServer –> Click Finish one done

3 Create a new Interface under this project named : FileServerInt via. Right Click under the project MyFileServer–> New–> Interface

copy-file-rmi2

4 Type the name of the class as : FileServerInt

copy-file-rmi3

5 Type in the following code the interface : FileServerInt

import java.rmi.*;

public interface FileServerInt extends Remote{

		public boolean login(FileClientInt c) throws RemoteException;
}

6 Create a new Interface under this project named : FileClientInt via. Right Click under the project MyFileServer–> New–> Interface

7 Type in the following code the interface : FileClientInt

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface FileClientInt extends Remote{

	public boolean sendData(String filename, byte[] data, int len) throws RemoteException;
	
	public String getName() throws RemoteException;
}

8 Create a new class named FileServer under the project. Right Click under the project MyFileServer–> New–> Class

copy-file-rmi4

9 Type the name of the class as : FileServer

copy-file-rmi5

10 Type the following code for the class FileServer

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;


public class FileServer  extends UnicastRemoteObject implements FileServerInt {
	
	private String file="";
	protected FileServer() throws RemoteException {
		super();
		// TODO Auto-generated constructor stub
	}

	public void setFile(String f){
		file=f;
	}
	
	public boolean login(FileClientInt c) throws RemoteException{
		/*
		 *
		 * Sending The File...
		 * 
		 */
		 try{
			 File f1=new File(file);			 
			 FileInputStream in=new FileInputStream(f1);			 				 
			 byte [] mydata=new byte[1024*1024];						
			 int mylen=in.read(mydata);
			 while(mylen>0){
				 c.sendData(f1.getName(), mydata, mylen);	 
				 mylen=in.read(mydata);				 
			 }
		 }catch(Exception e){
			 e.printStackTrace();
			 
		 }
		
		return true;
	}	
}

11 Create a new class named StartFileServer under the project. Right Click under the project MyFileServer–> New–> Class

12 Type the following code for the class StartFileServer

import java.rmi.Naming;

public class StartFileServer {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			
			java.rmi.registry.LocateRegistry.createRegistry(1099);
			
			FileServer fs=new FileServer();
			fs.setFile("itcrowd.avi");			
			Naming.rebind("rmi://192.168.1.7/abc", fs);
			System.out.println("File Server is Ready");
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

12 Edit the IP address to match your IP.

13 Set the name of the file you want to copy. Either you put the absolute path or just put a name for the file and put the file under your project as shown here:

copy-file-rmi6

13 Generate the stub for the FileServer

14 You can run the server now.

Client Section

1create a new Java Project via : File –> New –> Java Project

2 Set the name of the project as : MyFileClient –> Click Finish one done

3 Create a new Interface under this project named : FileClientInt via. Right Click under the project MyClientServer–> New–> Interface

4 Type in the following code the interface : FileClientInt

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface FileClientInt extends Remote{

	public boolean sendData(String filename, byte[] data, int len) throws RemoteException;
	
	public String getName() throws RemoteException;
}

5 Create a new Interface under this project named : FileServerInt via. Right Click under the project MyFileClient–> New–> Interface

6 Type in the following code the interface : FileServerInt

import java.rmi.*;

public interface FileServerInt extends Remote{

		public boolean login(FileClientInt c) throws RemoteException;
}

7 Create a new class named FileClient under the project. Right Click under the project MyFileClient–> New–> Class

8 Type the following code for the class FileClient

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;

public class FileClient  extends UnicastRemoteObject implements FileClientInt {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public String name;
	public  FileClient(String n) throws RemoteException {
		super();
		name=n;
	}

	public String getName() throws RemoteException{
		return name;
	}
    
	public boolean sendData(String filename, byte[] data, int len) throws RemoteException{
        try{
        	File f=new File(filename);
        	f.createNewFile();
        	FileOutputStream out=new FileOutputStream(f,true);
        	out.write(data,0,len);
        	out.flush();
        	out.close();
        	System.out.println("Done writing data...");
        }catch(Exception e){
        	e.printStackTrace();
        }
		return true;
	}
}

11 Create a new class named StartFileClient under the project. Right Click under the project MyFileClient–> New–> Class

12 Type the following code for the class StartFileClient

import java.rmi.Naming;
import java.util.Scanner;


public class StartFileClient {

	public static void main(String[] args) {
		try{
			FileClient c=new FileClient("imed");			
			FileServerInt server=(FileServerInt)Naming.lookup("rmi://192.168.1.7/abc");
			server.login(c);
			System.out.println("Listening.....");			
			Scanner s=new Scanner(System.in);			
			while(true){
				String line=s.nextLine();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}	

}

12 Edit the IP address to match the server IP.

Running the Server and Client

13 Generate the stub for the FileClient and FileServer

14 Put the both of the stub files under the client and the server.

13 Generate the stub for the FileClient