為什麼要用RMI
在這次的項目中,對於用戶端與伺服器之間的通訊,想了許多辦法,由於做的是富用戶端應用,最終將技術選定在了RMI和Java-sockets兩種之間,其中RMI的靈活性不高,用戶端和伺服器端都必須是java編寫,但使用比較方便,反觀java-sockets,雖然比較靈活,但需要自己規定伺服器端和用戶端之間的通訊協定。比較麻煩,幾經權衡,最終還是選擇RMI來進行伺服器-用戶端通訊
檔案上傳問題
在使用java-rmi的過程中,必然會遇到一個檔案上傳的問題,由於在rmi中無法傳輸檔案流(比如rmi中的方法參數不能是FileInputStream之類的),那麼我們只好選擇一種折中的辦法,就是先用FileInputStream將檔案讀到一個 Byte數組中,然後把這個Byte數組作為參數傳進RMI的方法中,然後在伺服器端將Byte數組還原為outputStream,這樣就能通過RMI 來傳輸檔案了
這樣做也有缺點,就是無法檢驗傳輸過來的資料的準確性。
下面我就一個執行個體來講解一下
FileClient
複製代碼 代碼如下:
package rmiupload;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class FileClient {
public FileClient() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
try {
FileDataService fileDataService = (FileDataService) Naming.lookup("rmi://localhost:9001/FileDataService");
fileDataService.upload("/Users/NeverDie/Documents/test.mp4", new FileClient().fileToByte("/Users/NeverDie/Music/test.mp4"));
} catch (MalformedURLException | RemoteException | NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//這個方法比較重要,通過這個方法把一個名為filename的檔案轉化為一個byte數組
private byte[] fileToByte(String filename){
byte[] b = null;
try {
File file = new File(filename);
b = new byte[(int) file.length()];
BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
is.read(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}
FileDataService
package rmiupload;
import java.net.URL;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FileDataService extends Remote{
//這裡的filename應該是該檔案存放在伺服器端的地址
public void upload(String filename, byte[] file) throws RemoteException;
}
FileDataService_imp
複製代碼 代碼如下:
package rmiupload;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.UnicastRemoteObject;
public class FileDataService_imp extends UnicastRemoteObject implements FileDataService{
public FileDataService_imp() throws RemoteException {
}
@Override
public void upload(String filename, byte[] fileContent) throws RemoteException{
File file = new File(filename);
try {
if (!file.exists())
file.createNewFile();
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
os.write(fileContent);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
; }
}
FileServer
複製代碼 代碼如下:
package rmiupload;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class FileServer {
FileDataService fileDataService;
public FileServer() {
try {
fileDataService = new FileDataService_imp();
LocateRegistry.createRegistry(9001);
Naming.rebind("rmi://localhost:9001/FileDataService", fileDataService);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new FileServer();
}
}