RMI客户端'无法投射'例外

时间:2018-03-16 14:13:01

标签: java registry rmi classcastexception stub

我正在尝试在RMI中编写一个从服务器程序中复制文件的客户端程序:

FileClientInt接口

<table matSort (matSortChange)="sortData($event)">
  <tr>
    <th [mat-sort-header]="mapped.id" [id]="mapped.id">{{mapped.header}}</th>   
 </tr>
</table>

FileServerInt接口

public interface FileClientInt extends Remote {

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

    public String getName() throws RemoteException;

}

}

FileClient Class

public interface FileServerInt extends Remote {


public boolean login(FileClientInt c) throws RemoteException;

StartFileClient Class

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

它给了我错误:

public class StartFileClient {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        FileClient c = new FileClient("out.txt");
        FileServerInt server  = (FileServerInt) Naming.lookup("rmi://192.168.2.15");            
        server.login(c);
        System.out.println("Listening.....");
        Scanner s = new Scanner(System.in);
        while (true) {
            String line = s.nextLine();
        }
    } catch (Exception e) {
        System.out.println("Error " + e.getMessage());
    }
}

我该如何解决此错误?任何人都可以帮助解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

您忘记将对象名称放在注册表查找字符串的末尾。所以你得到了注册表本身,而不是你正在查找的远程接口。

答案 1 :(得分:-1)

Naming.lookup(String name)返回Remote。您的FileServerIntRemote,但Remote不是FileServerInt。所以你不能把它投射到那个。

来源:https://docs.oracle.com/javase/7/docs/api/java/rmi/Naming.html