国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

一個相當經典的RMI實例源代碼及詳細說明

2019-11-18 13:07:25
字體:
來源:轉載
供稿:網友

  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)實現遠端接口中的參數。
  實現遠端接口中的參數的接口跟與實現其他任何接口沒什么不一樣的地方,如下所示
   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如下:
     
  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  
    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 + "http://" + fileName ;
        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 ;
  
      try{
        // check paramter
        if(fileName == null){
          throw new RemoteException("the paramter is null ");
        }
  
        // get path
        String filePath = this.currentDir + "http://" + 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){
        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() );
       
  //創建一個服務對象
        LoadFileService server = new LoadFileService();
        server.setCurrentDir("c://rmiexample");
       
        //將服務對象注冊到rmi注冊服務器上,而不是其他服務器
  //(因為LoadFileService extends UnicastRemoteObject)
        Naming.rebind("http://127.0.0.1:2000/LoadFileServer", server);
     }catch(Exception ex){
       System.out.println(ex.getLocalizedMessage());
       ex.printStackTrace();
     }
   }
  }
  
  注重:將對象注冊以后,不可關閉服務對象。
  
  編寫客戶端代碼
  客戶端代碼需要兩個步驟:
  (1)根據服務的名稱,查找服務對象
  (2)調用服務服務對象對應的方法完成工作
  在這個例子中,我們從客戶端上傳一個文件到服務器,并將服務器的一個文件下載下來。
  代碼如下:
   import java.io.IOExcep

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海淀区| 遂昌县| 铜梁县| 恩平市| 瑞金市| 长岭县| 伊川县| 乌苏市| 彰武县| 凌海市| 珠海市| 民勤县| 吉木乃县| 五寨县| 江西省| 阳信县| 高青县| 伊春市| 高雄县| 随州市| 根河市| 怀柔区| 栖霞市| 海淀区| 怀来县| 九龙县| 肇源县| 延川县| 红桥区| 东乌珠穆沁旗| 维西| 湘潭市| 集贤县| 西峡县| 南江县| 旌德县| 安溪县| 财经| 翁牛特旗| 霍邱县| 观塘区|