java rmi到底怎么用呢?能够给点具体的说明和例子么? 谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 RMI = Remote Method Invocation(远程方法调用)具体的细节也不是很清楚,平时都用的很少 一个相当经典的RMI实例源代码及详细RMI技术 下面以一个例子说明怎么使用RMI技术。这个例子演示了怎样将一个文件上传到服务器和怎样将一个文件从服务器上下载下来。 使用RMI技术共有6个步骤要走: (1)定义和实现远端接口中的参数 (2) 定义和实现远端接口 (3) 编写服务端代码 (4)编写客户端代码 (5)生成 RMI技术 下面以一个例子说明怎么使用RMI技术。这个例子演示了怎样将一个文件上传到服务器和怎样将一个文件从服务器上下载下来。 使用RMI技术共有6个步骤要走: (1)定义和实现远端接口中的参数 (2) 定义和实现远端接口 (3) 编写服务端代码 (4)编写客户端代码 (5)生成stub和skeltion ,并将stub打包到客户端jar中,将skeltion打包到服务端jar中 (6)启动rmiregistry , 并将服务注册到rmiregistry中,然后运行代码。下面就这六个方面说明rmi技术。 定义和实现远端接口中的参数 (1)定义远端接口中的参数 每一个远端接口中的参数都必须是可序列化的。那么,如何定义一个序列化的接口呢,简单,只需从java.io.Serializable继承即可,如下所示: import java.io.Serializable; public interface FileInformation extends Serializable { String getName(); byte[] getContent(); void setInformation(String name , byte[] content); }; (2)实现远端接口中的参数。 实现远端接口中的参数的接口跟与实现其他任何接口没什么不一样的地方,如下所示 中国网管论坛bbs.bitsCN.com public class FileInformationSev implements FileInformation { private String name = null ; private byte[] content = null ; public String getName() { return name ; } public byte[] getContent() { return content; } public void setInformation(String name, byte[] content) { this.name = name ; this.content = content ; } } 那么,为什么要序列化远端接口中的参数(返回值) ?这是因为需要将客户端的对象(参数)转化成byte stream,通过网络协议传输到服务端,再还原成服务端的对象进行调用。或者是需要将服务端的对象(返回值)转化成byte stream,通过网络协议传输到服务端,再还原成客户端的对象进行调用。 在 jdk中, java.lang包和java.util包下的类都已经实现了序列化,直接可以用在远程接口中作参数或返回值;所有的基本类型也可以直接用在远程接口中作参数或返回值; 定义和实现远端接口 (1)定义远端接口 远端接口必须从java.rmi.Remote继承;远端接口中的方法如果要throw异常,这个异常必须是java.rmi.RemoteException(或java.rmi.RemoteException的子类),否则,这个异常就不能被返回到客户端。Example如下: 中国网管联盟www.bitscn.com import java.rmi.Remote; import java.rmi.RemoteException; public interface LoadFile extends Remote { void upLoadFile(FileInformation fileInof) throws RemoteException; FileInformation downLoadFile(String filename) throws RemoteException ; } (2)实现远端接口 实现远端接口比较容易,跟其他接口的实现没有什么区别,如下所示: import java.rmi.Remote; import java.rmi.RemoteException; import java.io.IOException; import java.io.File; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.rmi.server.UnicastRemoteObject; public class LoadFileService extends UnicastRemoteObject implements LoadFile { private String currentDir= null ; // this contruction is needed 网管网bitsCN.com public LoadFileService() throws RemoteException { super(); } public void setCurrentDir(String currentDir){ this.currentDir = currentDir ; } public void upLoadFile(FileInformation fileInfo) throws RemoteException{ BufferedOutputStream output = null ; try{ // check paramter if(fileInfo == null ){ throw new RemoteException("the paramter is null "); } //check fileName and content String fileName = fileInfo.getName() ; byte [] content = fileInfo.getContent() ; if(fileName == null || content == null ){ throw new RemoteException("the file or the content is null "); } //create file String filePath = this.currentDir + "\\" + fileName ; 网管联盟www.bitsCN.com File file = new File(filePath); if(!file.exists()){ file.createNewFile(); } //save the content to the file output = new BufferedOutputStream(new FileOutputStream(file)); output.write(content); }catch(RemoteException ex){ throw ex ; }catch(Exception ex){ throw new RemoteException(ex.getLocalizedMessage()); }finally{ if(output != null ){ try{ output.close(); output = null ; }catch(Exception ex){ } } } } public FileInformation downLoadFile(String fileName) throws RemoteException { FileInformation fileInfo = null ; BufferedInputStream input = null ; 网管网bitsCN_com try{ // check paramter if(fileName == null){ throw new RemoteException("the paramter is null "); } // get path String filePath = this.currentDir + "\\" + fileName ; File file = new File(filePath); if(!file.exists()){ throw new RemoteException("the file whose name is " + fileName + " not existed "); } // get content byte[] content = new byte[(int)file.length()]; input = new BufferedInputStream(new FileInputStream(file)); input.read(content); // set file name and content to fileInfo fileInfo = new FileInformationSev(); fileInfo.setInformation(fileName , content); }catch(RemoteException ex){ 54ne.com throw ex ; }catch(Exception ex){ throw new RemoteException(ex.getLocalizedMessage()); }finally{ if(input != null ){ try{ input.close(); input = null ; }catch(Exception ex){ } } } return fileInfo ; } } 编写服务端代码 服务端代码主要有3个步骤: (1)加载安全管理器 (2)创建一个服务对象 (3)将这个服务对象注册到命名服务上 : import java.rmi.RMISecurityManager; import java.rmi.Naming; public class RMIServer { public static void main(String[] args) { try{ //加载安全管理器 System.setSecurityManager(new RMISecurityManager() ); //创建一个服务对象 中国网管联盟www.bitscn.com LoadFileService server = new LoadFileService(); server.setCurrentDir("c:\\rmiexample"); //将服务对象注册到rmi注册服务器上,而不是其他服务器 //(因为LoadFileService extends UnicastRemoteObject) Naming.rebind("//127.0.0.1:2000/LoadFileServer", server); }catch(Exception ex){ System.out.println(ex.getLocalizedMessage()); ex.printStackTrace(); } } } 注意:将对象注册以后,不可关闭服务对象。 编写客户端代码 客户端代码需要两个步骤: (1)根据服务的名称,查找服务对象 (2)调用服务服务对象对应的方法完成工作 在这个例子中,我们从客户端上传一个文件到服务器,并将服务器的一个文件下载下来。 代码如下: import java.io.IOExcep 资料不完整,楼主还是自己动手Google/baidu一下吧 关于static的一个简单问题 关于中文的问题 有对JSP有兴趣的朋友吗 加我msn 我们互相讨论问题 java中的">>>"在c#中如何实现 一个比较菜的问题: 怎样让JDialog窗口无法改变大小? 如何让字总保持在屏幕中央? 帮我调个小程序 java程序员面试中的多线程问题 求一个生成随机随机数的方法 这个程序中的文本段里内容怎么不变呢? [面试题] 在整个窗口内有办法判断鼠标是不是被按下在那里呢?大家帮忙下。 对WORD文档进行精细化操作,使用什么方式比较好?
具体的细节也不是很清楚,平时都用的很少
RMI技术
下面以一个例子说明怎么使用RMI技术。这个例子演示了怎样将一个文件上传到服务器和怎样将一个文件从服务器上下载下来。
使用RMI技术共有6个步骤要走: (1)定义和实现远端接口中的参数 (2) 定义和实现远端接口 (3) 编写服务端代码 (4)编写客户端代码 (5)生成stub和skeltion ,并将stub打包到客户端jar中,将skeltion打包到服务端jar中 (6)启动rmiregistry , 并将服务注册到rmiregistry中,然后运行代码。下面就这六个方面说明rmi技术。
定义和实现远端接口中的参数
(1)定义远端接口中的参数
每一个远端接口中的参数都必须是可序列化的。那么,如何定义一个序列化的接口呢,简单,只需从java.io.Serializable继承即可,如下所示:
import java.io.Serializable;
public interface FileInformation extends Serializable {
String getName();
byte[] getContent();
void setInformation(String name , byte[] content);
};
(2)实现远端接口中的参数。
实现远端接口中的参数的接口跟与实现其他任何接口没什么不一样的地方,如下所示
中国网管论坛bbs.bitsCN.com public class FileInformationSev implements FileInformation {
private String name = null ;
private byte[] content = null ;
public String getName() {
return name ;
}
public byte[] getContent() {
return content;
}
public void setInformation(String name, byte[] content) {
this.name = name ;
this.content = content ;
}
}
那么,为什么要序列化远端接口中的参数(返回值) ?这是因为需要将客户端的对象(参数)转化成byte stream,通过网络协议传输到服务端,再还原成服务端的对象进行调用。或者是需要将服务端的对象(返回值)转化成byte stream,通过网络协议传输到服务端,再还原成客户端的对象进行调用。
在 jdk中, java.lang包和java.util包下的类都已经实现了序列化,直接可以用在远程接口中作参数或返回值;所有的基本类型也可以直接用在远程接口中作参数或返回值;
定义和实现远端接口
(1)定义远端接口
远端接口必须从java.rmi.Remote继承;远端接口中的方法如果要throw异常,这个异常必须是java.rmi.RemoteException(或java.rmi.RemoteException的子类),否则,这个异常就不能被返回到客户端。Example如下:
中国网管联盟www.bitscn.com
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface LoadFile extends Remote {
void upLoadFile(FileInformation fileInof) throws RemoteException;
FileInformation downLoadFile(String filename) throws RemoteException ;
}
(2)实现远端接口
实现远端接口比较容易,跟其他接口的实现没有什么区别,如下所示:
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.io.IOException;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.rmi.server.UnicastRemoteObject;
public class LoadFileService extends UnicastRemoteObject implements LoadFile {
private String currentDir= null ;
// this contruction is needed 网管网bitsCN.com
public LoadFileService() throws RemoteException {
super();
}
public void setCurrentDir(String currentDir){
this.currentDir = currentDir ;
}
public void upLoadFile(FileInformation fileInfo) throws RemoteException{
BufferedOutputStream output = null ;
try{
// check paramter
if(fileInfo == null ){
throw new RemoteException("the paramter is null ");
}
//check fileName and content
String fileName = fileInfo.getName() ;
byte [] content = fileInfo.getContent() ;
if(fileName == null || content == null ){
throw new RemoteException("the file or the content is null ");
}
//create file
String filePath = this.currentDir + "\\" + fileName ; 网管联盟www.bitsCN.com
File file = new File(filePath);
if(!file.exists()){
file.createNewFile();
}
//save the content to the file
output = new BufferedOutputStream(new FileOutputStream(file));
output.write(content);
}catch(RemoteException ex){
throw ex ;
}catch(Exception ex){
throw new RemoteException(ex.getLocalizedMessage());
}finally{
if(output != null ){
try{
output.close();
output = null ;
}catch(Exception ex){
}
}
}
}
public FileInformation downLoadFile(String fileName) throws RemoteException {
FileInformation fileInfo = null ;
BufferedInputStream input = null ; 网管网bitsCN_com
try{
// check paramter
if(fileName == null){
throw new RemoteException("the paramter is null ");
}
// get path
String filePath = this.currentDir + "\\" + fileName ;
File file = new File(filePath);
if(!file.exists()){
throw new RemoteException("the file whose name is " + fileName + " not existed ");
}
// get content
byte[] content = new byte[(int)file.length()];
input = new BufferedInputStream(new FileInputStream(file));
input.read(content);
// set file name and content to fileInfo
fileInfo = new FileInformationSev();
fileInfo.setInformation(fileName , content);
}catch(RemoteException ex){
54ne.com throw ex ;
}catch(Exception ex){
throw new RemoteException(ex.getLocalizedMessage());
}finally{
if(input != null ){
try{
input.close();
input = null ;
}catch(Exception ex){
}
}
}
return fileInfo ;
}
}
编写服务端代码
服务端代码主要有3个步骤:
(1)加载安全管理器
(2)创建一个服务对象
(3)将这个服务对象注册到命名服务上
:
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
public class RMIServer {
public static void main(String[] args) {
try{
//加载安全管理器
System.setSecurityManager(new RMISecurityManager() );
//创建一个服务对象
中国网管联盟www.bitscn.com
LoadFileService server = new LoadFileService();
server.setCurrentDir("c:\\rmiexample");
//将服务对象注册到rmi注册服务器上,而不是其他服务器
//(因为LoadFileService extends UnicastRemoteObject)
Naming.rebind("//127.0.0.1:2000/LoadFileServer", server);
}catch(Exception ex){
System.out.println(ex.getLocalizedMessage());
ex.printStackTrace();
}
}
}
注意:将对象注册以后,不可关闭服务对象。
编写客户端代码
客户端代码需要两个步骤:
(1)根据服务的名称,查找服务对象
(2)调用服务服务对象对应的方法完成工作
在这个例子中,我们从客户端上传一个文件到服务器,并将服务器的一个文件下载下来。
代码如下:
import java.io.IOExcep
资料不完整,楼主还是自己动手Google/baidu一下吧