javaclient-serverrmifilenotfoundexceptionfilereference

File Not Found exception --- having issue with passing File-Reference to RMI-Client


I was trying yesterday to communicate one of my client with my system being the Remote Server and a Client system with Ubuntu 14.04.

The code was about File transfer from Sever to client by passing File reference to the client and client downloading it using the reference,. We were connected to each other through LAN and we able to ping successfully each other's IP(my---10.0.0.2 and his---10.0.0.1).

But,when we ran this code with rmiregistry & on my CentOS Linux and then running Server Side class on my system & his running of java Client class, our connection was done,but, we were unable to pass the file from my system to his system...

The Exception thrown was :-

java.io.FileNotFoundException at Java_Client_1 --- File in = u1.Uploads();
// I have printed the comment on the line having error in the class Java_client_1 class' `main()` method.

Common interface residing on client and server :-

import java.io.*;
import java.rmi.*;

public interface Upload_1 extends Remote {
File Uploads() throws RemoteException;
}

Client Class code :-

import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;

public class Java_Client_1 {
public static void main(String[] args) {
    try{
       Registry registry=LocateRegistry.getRegistry("10.0.0.2",2000); 
       Upload_1 u1=(Upload_1)registry.lookup("Uploading");
       //File in=new File("/home/ssuman/Public/KALI_LINUX/MyFavourite","something.mp4");
       File out=new File("/home/asad/Music","tymaPaulMultithreaded.mp4");
       System.out.println("Transferring File---Please be Patient...\n");
       File in = u1.Uploads();   //  exception thrown here,please check...
       FileInputStream fis =  new FileInputStream(in);
       FileOutputStream fos = new FileOutputStream(out);
       byte[] buf = new byte[4096];
       int i = 0;
       while ((i = fis.read(buf)) != -1) {
        fos.write(buf, 0, i);
    }
    if (fis != null) fis.close();
    if (fos != null) fos.close();
    System.out.println("File Successfully Transferred...");
    System.out.println("Congratulations,RMI is working successfully!!!");
    }
    catch(Exception e){
        e.printStackTrace();
    }     
  }   
}

Server Side Implementer Class :-

import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class Java_Uploader_1 extends UnicastRemoteObject implements Upload_1 {   
public Java_Uploader_1() throws RemoteException{  }

@Override
public File Uploads() throws RemoteException{ 
 File in = new File("/home/ssuman/Public/KALI_LINUX/MyFavourite","something.mp4");
 return in;
} 
}

Server Class code :-

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Java_Server_1 {
public static void main(String[] args) {
    try{
        Registry registry= LocateRegistry.createRegistry(2000);
        System.out.println("Ahh,Server Finally Started---GO,GO,GO...\n");
        Java_Uploader_1 ju1=new Java_Uploader_1();
        registry.rebind("Uploading",ju1);
        System.out.println("Service Bound...");
      }
    catch(Exception e){
        System.out.println("An error occured trying to bind the object to the registry.\n"+e);
    }

   }
}

MY QUESTION:-

Kindly help to resolve this issue OR any alternative for letting remote-client download the file present on my system. And,yes, i want all this to be done using Java RMI, not from CORBA or any othe technology. Thanks in advance for all the commentators and answer posters.


Solution

  • You have to transfer the content of the file. To do that you have to change the remote interface signature:

    public interface Upload_1 extends Remote {
      void upload(byte[] content) throws RemoteException;
    }
    

    The client send a byte array with the content of the file, and the server takes it and writes to disk.

    The problem is when you use large files, the javavm will throw OOME, so you have to simulate streaming over RMI.

    Another alternative is using an existing library, take a look at http://openhms.sourceforge.net/rmiio/