前段时间完成的一个实习项目,涉及到了利用java的RMI技术,从Server下载文件到Client,这里还是做一下笔记,分享经验,同时也方便以后回顾。
1. 在eclipse中创建以下的目录结构
2. 说明每一个class的作用
2.1 com.download.client 包下的类
2.1.1 Client类
package com.download.client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import com.download.common.IDownload;
public class Client {
public static void main(String[] args){
try {
Registry registry = LocateRegistry.getRegistry("localhost", 9001);
IDownload downloadService = (IDownload) registry.lookup("DownloadService");
byte[] fileBytes = downloadService.download("C:\\FileTest\\src\\1.jpg"); //下载位于服务端的文件地址
FileUtil.saveAs("C:\\FileTest\\dst\\1new.jpg", fileBytes); //存放于客户端的文件地址
System.out.println("Download successful");
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.1.2 FileUtil类(将从服务端下载的字节数组存放到客户端)
package com.download.client;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public class FileUtil {
public static void saveAs(String dstFile, byte[] bytes) throws Exception{
File file = new File(dstFile);
try {
if ( !file.exists() ){
file.createNewFile();
}
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
os.write(bytes);
os.close();
} catch (Exception e) {
throw e;
}
}
}
2.2 com.download.common包下的类
2.2.1 IDownload接口(远程接口)
package com.download.common;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IDownload extends Remote{
public byte[] download(String srcFile) throws RemoteException;
}
2.3 com.download.server包下的类
2.3.1 Server类
package com.download.server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import com.download.common.IDownload;
public class Server {
IDownload downloadService;
public Server() {
try {
downloadService = new DownloadService();
LocateRegistry.createRegistry(9001);
Naming.rebind("rmi://localhost:9001/DownloadService", downloadService);
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
new Server();
System.out.println("ok");
}
}
2.3.2 DownloadService类(实现远程接口的类)
<pre name="code" class="java">package com.download.server;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import com.download.common.IDownload;
public class DownloadService extends UnicastRemoteObject implements IDownload{
/**
*
*/
private static final long serialVersionUID = 1L;
protected DownloadService() throws RemoteException {
super();
}
public byte[] download(String srcFile) throws RemoteException {
byte[] bytes = null;
try {
File srcfile = new File(srcFile);
bytes = new byte[(int) srcfile.length()];
BufferedInputStream is = new BufferedInputStream(new FileInputStream(srcfile));
is.read(bytes);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}
}
3. 创建完整个demo的代码之后,先启动Server.java类,再在Client.java类中设置文件的源地址和目的地址,最后运行Client.java就可以实现功能了。