精华全集 冲广角币 获取广角币的更多方法
【名称】:六步教你学会简单RMI
【作者】:江苏 无锡 缪小东
【格式】:PDF
【页数】:13
【语言】:中文
【出版社】:
【出版日期】:
【摘要或目录】:
(以下所有java文件、.class文件和policy.txt文件都在c盘根目录哦!101.txt在c盘的子目录11下哦!一定要放对!!!) 一、 定义远程接口 远程接口就是远程机器上可供客户使用的方法的集合。很幸运它用java语言的接口表示!我们定义这样一个接口只有一个下载远程机器上的指定名称的文件。 //FileServerInterface.java import java.rmi.Remote; import java.rmi.RemoteException; public interface FileServerInterface extends Remote { public byte[] download(String Filename)throws RemoteException ; } 二、实现远程接口 实现上面远程接口的方法,同时继承UnicastRemoteObject类! //FileServerImpl.java import java.io.*; import java.rmi.*; import java.rmi.server.UnicastRemoteObject ; public class FileServerImpl extends UnicastRemoteObject implements FileServerInterface{ private static final String initDir = "c://11//"; public FileServerImpl( ) throws RemoteException{ super(); } public byte[] download(String filename){ try{ File file = new File(initDir + filename); byte[] buffer = new byte[(int)file.length()]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); bis.read(buffer,0,buffer.length); bis.close(); return buffer ; }catch(Exception e ){ System.out.println("FileServerImpl: " + e.getMessage()); e.printStackTrace();
2
return null ; } } }