麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

一個相當經典的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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄网站在线免费 | 久久久aa| 久草在线免费资源站 | 成人免费激情视频 | 黄在线观看 | 日本欧美一区二区三区在线播 | 国产羞羞视频在线观看 | 国产在线1区 | 欧美一区二区黄 | 国产精品一区二区x88av | 黄污网站在线 | 欧美亚洲黄色 | 91福利社在线 | 久久精品久久久久 | 精品国产一区三区| 免费中文视频 | 国产流白浆高潮在线观看 | 国产xxxxx在线观看 | 黄色一级电影网 | 国产午夜亚洲精品午夜鲁丝片 | 99麻豆久久久国产精品免费 | 国产毛片网站 | 最新91在线视频 | 久久久久久久免费视频 | 国产亚洲精品久久久久婷婷瑜伽 | 亚洲一区二区 | 久久精品小短片 | 国产成人免费精品 | 日本aⅴ在线 | 性欧美极品xxxx欧美一区二区 | 福利在线免费 | 香蕉视频18 | 国产无遮挡一区二区三区毛片日本 | 成人福利在线视频 | 国产一级免费视频 | 蜜桃网在线 | 欧美成人免费在线视频 | 久久丝袜脚交足黄网站免费 | 国产品久久 | 欧美一级在线免费 | 在线播放免费播放av片 |