gpt4 book ai didi

java - 如何使用java.io.File浏览RMI服务器磁盘文件系统?

转载 作者:行者123 更新时间:2023-11-30 04:55:58 25 4
gpt4 key购买 nike

我想要一个 RMI 服务器/客户端系统,您可以在其中使用客户端浏览服务器上的文件。在本例中,服务器是 Debian,客户端在 Windows 上运行。

我尝试让服务器保存一个指向当前看到的目录的 File 对象,并在客户端的 List 中显示该目录中的所有文件。

问题是,当我调用返回 file.listFiles() 的方法时,我没有在服务器上获取文件,而是在客户端上获取文件或 FileNotFoundException就好像服务器在客户端上运行一样。 Java File API 似乎使用运行客户端的计算机上的根目录,而不是服务器上的根目录。

更简单地说:我希望客户端上有一个文件浏览器向我显示服务器文件系统。

编辑:

public class ClientMain {

/**
* @param args
*/
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
View view = new View();
view.setVisible(true);
try {
String name = "Remote";
Registry registry = LocateRegistry.getRegistry("127.0.0.1");
IRemote model = (IRemote) registry.lookup(name);
view.setModel(model);
view.update();
} catch (Exception e) {
System.err.println("Remote Exception");
e.printStackTrace();
}
}

}

public class View extends JFrame implements IView{
JList list;
IRemote model;
public View() {
super();
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
list = new JList();
this.add(list);
}
public IRemote getModel() {
return model;
}
public void setModel(IRemote model) {
this.model = model;
}

public void update(){
try {
this.list.setListData(model.getFileList());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


public interface IRemote extends Remote {
public String[] getFileList() throws RemoteException;
}

public class Model implements IRemote{
File current;

public Model() {
super();
current = new File(".");
}

public String[] getFileList() {
return current.list();
}

public void setCurrentDirectory(String current) {
this.current = new File(current);
}




}
public class ServerMain {
public static void main(String[] args) {
new ServerMain();
}

public ServerMain() {
super();
Model model = new Model();
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Remote";
IRemote stub = (IRemote) UnicastRemoteObject.exportObject(model, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind(name, stub);
} catch (Exception e) {
System.err.println("Controller exception:");
e.printStackTrace();
}
}


}

这就是我正在尝试做的事情。这里服务器将模型绑定(bind)到注册表。客户端查找模型并将模型提供给 View , View 从模型调用 getFileList()。用“.”文件我得到程序所在的目录。因为它是相对的,所以我获取运行客户端程序的客户端上的所有文件。如果我使用非相对目录,我会收到 FileNotFoundException,因为客户端没有此路径。希望这能让它更加清晰。

最佳答案

File 不是远程对象。无论它告诉服务器什么都适用于服务器。您可以通过 RMI 将其发送到客户端,因为它是可序列化的,并且它不带有任何外部状态,例如服务器文件系统的状态。

关于java - 如何使用java.io.File浏览RMI服务器磁盘文件系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8539016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com