為什麼要用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();
}
}