RMI技術
下面以一個例子說明怎麼使用RMI技術
使用RMI技術共有
定義和實現遠端接口中的參數
(
每一個遠端接口中的參數都必須是可序列化的
import java
public interface FileInformation extends Serializable {
String getName();
byte[] getContent();
void setInformation(String name
};
(
實現遠端接口中的參數的接口跟與實現其他任何接口沒什麼不一樣的地方
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
this
ntent = content ;
}
}
那麼
在 jdk中
定義和實現遠端接口
(
遠端接口必須從java
import java
import java
public interface LoadFile extends Remote {
void upLoadFile(FileInformation fileInof) throws RemoteException;
FileInformation downLoadFile(String filename) throws RemoteException ;
}
(
實現遠端接口比較容易
import java
import java
import java
import java
import java
import java
import java
import java
import java
public class LoadFileService extends UnicastRemoteObject implements LoadFile {
private String currentDir= null ;
// this contruction is needed
public LoadFileService() throws RemoteException {
super();
}
public void setCurrentDir(String currentDir){
this
}
public void upLoadFile(FileInformation fileInfo) throws RemoteException{
BufferedOutputStream output = null ;
try{
// check paramter
if(fileInfo == null ){
throw new RemoteException(
}
//check fileName and content
String fileName = fileInfo
byte [] content = fileInfo
if(fileName == null || content == null ){
throw new RemoteException(
}
//create file
String filePath = this
File file = new File(filePath);
if(!file
file
}
//save the content to the file
output = new BufferedOutputStream(new FileOutputStream(file));
output
}catch(RemoteException ex){
throw ex ;
}catch(Exception ex){
throw new RemoteException(ex
}finally{
if(output != null ){
try{
output
output = null ;
}catch(Exception ex){
}
}
}
}
public FileInformation downLoadFile(String fileName) throws RemoteException {
FileInformation fileInfo = null ;
BufferedInputStream input = null ;
try{
// check paramter
if(fileName == null){
throw new RemoteException(
}
// get path
String filePath = this
File file = new File(filePath);
if(!file
throw new RemoteException(
}
// get content
byte[] content = new byte[(int)file
input = new BufferedInputStream(new FileInputStream(file));
input
// set file name and content to fileInfo
fileInfo = new FileInformationSev();
fileInfo
}catch(RemoteException ex){
throw ex ;
}catch(Exception ex){
throw new RemoteException(ex
}finally{
if(input != null ){
try{
input
input = null ;
}catch(Exception ex){
}
}
}
return fileInfo ;
}
}
編寫服務端代碼
服務端代碼主要有
(
(
(
import java
import java
public class RMIServer {
public static void main(String[] args) {
try{
//加載安全管理器
System
//創建一個服務對象
LoadFileService server = new LoadFileService();
server
//將服務對象注冊到rmi注冊服務器上
//(因為LoadFileService extends UnicastRemoteObject)
Naming
}catch(Exception ex){
System
ex
}
}
}
注意
編寫客戶端代碼
客戶端代碼需要兩個步驟
(
(
在這個例子中
代碼如下
import java
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27945.html