Rmi Java Code for File Upload and Download
This is a simple Coffee project using RMI to show you the basics of how to transfer a file from one computer to another. The awarding has two sections. i) Server and two)Client.
one) Server Section.
1 Open Eclipse and create a new Java Project via : File –> New –> Coffee Project
2 Set the proper name of the project as : MyFileServer –> Click End 1 done
3 Create a new Interface under this projection named : FileServerInt via. Right Click nether the projection MyFileServer–> New–> Interface
4 Type the name of the class as : FileServerInt
five 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 nether this project named : FileClientInt via. Correct 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 projection MyFileServer–> New–> Course
9 Type the name of the form as : FileServer
x Blazon the following code for the class FileServer
import java.io.File; import java.io.FileInputStream; import coffee.io.FileOutputStream; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import coffee.util.*; public course 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; } }
xi Create a new form named StartFileServer under the projection. Right Click under the projection MyFileServer–> New–> Class
12 Type the following lawmaking for the grade StartFileServer
import coffee.rmi.Naming; public grade StartFileServer { public static void main(Cord[] args) { // TODO Automobile-generated method stub try{ coffee.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 Set"); }catch(Exception e){ e.printStackTrace(); } } }
12 Edit the IP accost to lucifer your IP.
13 Gear up the name of the file you lot want to copy. Either you put the accented path or just put a name for the file and put the file under your project every bit shown hither:
13 Generate the stub for the FileServer
14 You can run the server now.
Client Department
onecreate a new Java Project via : File –> New –> Java Project
2 Set the proper name of the project as : MyFileClient –> Click Finish i done
three Create a new Interface under this project named : FileClientInt via. Right Click nether the project MyClientServer–> New–> Interface
4 Blazon in the following code the interface : FileClientInt
import java.rmi.Remote; import java.rmi.RemoteException; public interface FileClientInt extends Remote{ public boolean sendData(Cord filename, byte[] data, int len) throws RemoteException; public Cord getName() throws RemoteException; }
5 Create a new Interface nether this project named : FileServerInt via. Correct Click under the projection MyFileClient–> New–> Interface
6 Type in the following lawmaking the interface : FileServerInt
import java.rmi.*; public interface FileServerInt extends Remote{ public boolean login(FileClientInt c) throws RemoteException; }
seven Create a new class named FileClient under the projection. Correct Click under the projection MyFileClient–> New–> Class
viii Blazon the post-obit lawmaking for the course FileClient
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import coffee.util.*; public class FileClient extends UnicastRemoteObject implements FileClientInt { /** * */ individual static final long serialVersionUID = 1L; public String name; public FileClient(String n) throws RemoteException { super(); name=north; } public String getName() throws RemoteException{ render 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,truthful); out.write(data,0,len); out.flush(); out.shut(); System.out.println("Done writing information..."); }catch(Exception e){ e.printStackTrace(); } render true; } }
eleven Create a new grade named StartFileClient nether the project. Right Click under the project MyFileClient–> New–> Course
12 Type the following code for the class StartFileClient
import java.rmi.Naming; import java.util.Scanner; public form StartFileClient { public static void primary(String[] args) { try{ FileClient c=new FileClient("imed"); FileServerInt server=(FileServerInt)Naming.lookup("rmi://192.168.one.seven/abc"); server.login(c); System.out.println("Listening....."); Scanner due south=new Scanner(Arrangement.in); while(true){ Cord line=s.nextLine(); } }grab(Exception e){ eastward.printStackTrace(); } } }
12 Edit the IP address to match the server IP.
Running the Server and Client
thirteen Generate the stub for the FileClient and FileServer
fourteen Put the both of the stub files under the customer and the server.
thirteen Generate the stub for the FileClient
Source: http://www.ejbtutorial.com/java-rmi/how-to-transfer-or-copy-a-file-between-computers-using-java-rmi
Postar um comentário for "Rmi Java Code for File Upload and Download"