【问题标题】:File Not Found exception --- having issue with passing File-Reference to RMI-ClientFile Not Found 异常 --- 将 File-Reference 传递给 RMI-Client 时遇到问题
【发布时间】:2014-11-15 07:32:56
【问题描述】:

我昨天尝试将我的一个客户端与我的系统进行通信,即远程服务器和一个带有 Ubuntu 14.04 的客户端系统。

代码是关于从服务器到客户端的文件传输,方法是将文件引用传递给客户端,客户端使用引用下载它。我们通过 LAN 相互连接,我们能够成功 ping 到对方的 IP(我的---10.0.0.2 和他的---10.0.0.1)。

但是,当我们在我的 CentOS Linux 上使用 rmiregistry & 运行这段代码,然后在我的系统上运行服务器端类并运行 java 客户端类时,我们的连接已经完成,但是,我们无法从我的系统到他的系统...

抛出的异常是:-

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.

驻留在客户端和服务器上的通用接口 :-

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

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

客户端类代码:-

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();
    }     
  }   
}

服务器端实现类:-

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;
} 
}

服务器类代码:-

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);
    }

   }
}

我的问题:-

请帮助解决此问题或让远程客户端下载我系统上存在的文件的任何替代方法。而且,是的,我希望所有这些都使用 Java RMI 来完成,而不是来自 CORBA 或任何其他技术。提前感谢所有评论员和回答海报。

【问题讨论】:

    标签: java client-server rmi filenotfoundexception filereference


    【解决方案1】:

    您必须传输文件的内容。 为此,您必须更改远程接口签名:

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

    客户端发送一个包含文件内容的字节数组,服务器获取并写入磁盘。

    问题是当你使用大文件时,javavm 会抛出 OOME,所以你必须模拟 RMI 上的流。

    另一种选择是使用现有的库,看看http://openhms.sourceforge.net/rmiio/

    【讨论】:

      【解决方案2】:

      File 本质上是本地文件系统上文件name 的包装器。

      没有理由期望它对远程对等方有任何意义。

      您需要发送内容,而不仅仅是名称

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-22
        • 2019-06-10
        • 1970-01-01
        • 1970-01-01
        • 2019-06-11
        • 2022-01-16
        • 2013-11-14
        • 2014-07-29
        相关资源
        最近更新 更多