Create a distributed application using RMI, where an RMI client can download a text file from the RMI server. Also identify the design pattern being used.
Java Program -
RemoteInterface.java -
package com.gpm.ex3;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
public interface RemoteInterface extends Remote
{
public boolean CheckAvailability(String filename) throws RemoteException;
public ArrayList<String> DownloadFileData() throws RemoteException,FileNotFoundException,IOException;
}
FileServant.java -
package com.gpm.ex3;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
public class FileServant extends UnicastRemoteObject implements RemoteInterface {
String path = "D:/Manthan/";
String fullPath;
public FileServant() throws RemoteException
{
super();
}
@Override
public boolean CheckAvailability (String filename) throws RemoteException
{
boolean isAvailable =false;
fullPath = path.concat(filename);
File file = new File(fullPath);
if(file.exists())
{
if (file.canRead())
{
System.out.println("File is available");
isAvailable = true;
}
}
return isAvailable;
}
@Override
public ArrayList<String> DownloadFileData() throws RemoteException, FileNotFoundException, IOException
{
ArrayList<String>data = new ArrayList<String>();
FileReader fileReader = new FileReader(fullPath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
System.out.println("Reading File>>");
String currentLine = bufferedReader.readLine();
while(currentLine != null)
{
data.add(currentLine);
currentLine = bufferedReader.readLine();
}
System.out.println(("Read complete>> "));
fileReader.close();
System.out.println("Transfering file to the client>>");
return data;
}
}
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.io.File;
0 Comments