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 + "\\" + 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 + "\\" + 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("//127.0.0.1:2000/LoadFileServer", server); }catch(Exception ex){ System.out.println(ex.getLocalizedMessage()); ex.printStackTrace(); } } }
注意:将对象注册以后,不可关闭服务对象。
==编写客户端代码==
客户端代码需要两个步骤:
(1)根据服务的名称,查找服务对象
(2)调用服务服务对象对应的方法完成工作
在这个例子中,我们从客户端上传一个文件到服务器,并将服务器的一个文件下载下来。
代码如下:
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.Naming; public class RMIClient { private static String getNameByPath(String path){ StringBuffer strBuffer = new StringBuffer(path); int index = strBuffer.lastIndexOf("\\"); String name = strBuffer.substring(index+1); return name ; } public static void uploadFile(LoadFile load){ BufferedInputStream input = null ; FileInformation fileInfo = null ; try{ byte[] buffer = new byte[100]; System.out.println("please given the uploadfile path, just like c:\\rmiexample\\test.xml"); System.in.read(buffer); String uploadPath = new String(buffer) ; uploadPath = uploadPath.trim(); File file = new File(uploadPath); byte[] content = new byte[(int)file.length()]; input = new BufferedInputStream(new FileInputStream(file)); input.read(content); fileInfo = new FileInformationSev(); fileInfo.setInformation(getNameByPath(uploadPath), content ); load.upLoadFile(fileInfo); }catch(Exception ex){ System.out.println(ex.getLocalizedMessage()); ex.printStackTrace(); }finally{ try{ if( input != null ){ input.close(); input = null ; } }catch(Exception ex){ } } } public static void downLoadFile(LoadFile load){ BufferedOutputStream output = null ; FileInformation fileInfo = null ; try{ byte [] fileBuffer = new byte[100]; System.out.println("please given the file's name that need download, format just like test.xml"); System.in.read(fileBuffer); String downFileName = new String(fileBuffer) ; downFileName = downFileName.trim(); byte[] pathBuffer = new byte[100]; System.out.println("please given the local position , format just like c:\\rmiexample"); System.in.read(pathBuffer); String localPath = new String(pathBuffer) ; localPath = localPath.trim(); fileInfo= load.downLoadFile(downFileName); File file = new File(localPath+"\\"+downFileName ); if(!file.exists()){ file.createNewFile(); } output = new BufferedOutputStream(new FileOutputStream(file)); output.write(fileInfo.getContent()); output.flush(); }catch(Exception ex){ System.out.println(ex.getLocalizedMessage()); ex.printStackTrace(); }finally{ try{ if( output != null ){ output.close(); output = null ; } }catch(Exception ex){ } } } public static void main(String[] args) { try{ String serverName = "//192.168.39.168:2000/LoadFileServer"; //2 get the server LoadFile load = (LoadFile)Naming.lookup(serverName); //3 upload file RMIClient.uploadFile(load); //4 download file RMIClient.downLoadFile(load); }catch(Exception ex){ System.out.println(ex.getLocalizedMessage()); ex.printStackTrace(); } } }
==根据服务端的实现生成stub和skeltion , 并将stub打包到客户端jar中,将sub和skeltion打包到服务端jar中==
* 调用如下命令生成stub和skeltion
rmic LoadFileService
生成的文件名如下:
LoadFileService_Skel.class
LoadFileService_Stub.class
* 调用如下命令将LoadFileService_Stub.class和其他客户端文件到客户端jar中
jar cvf Client.jar FileInformation.class
jar uvf Client.jar FileInformationSev.class
jar uvf Client.jar LoadFile.class
jar uvf Client.jar RMIClient.class
jar uvf Client.jar LoadFileService_Stub.class
调用如下命令将LoadFileService_ Skel.class和其他服务端文件到服务端jar中
jar cvf Server.jar FileInformation.class
jar uvf Server.jar FileInformationSev.class
jar uvf Server.jar LoadFile.class
jar uvf Server.jar LoadFileService.class
jar uvf Server.jar LoadFileService_Skel.class
jar uvf Client.jar LoadFileService_Stub.class
jar uvf Server.jar RMIServer.class
==启动rmiregistry , 并将服务注册到rmiregistry中,然后运行代码==
(1)使用rmiregistry [port] 启动rmi服务
rmiregistry 2000
(2)编写授权代码如下所示,并存为c:\rmiexample\grant.txt
grant {
permission java.net.SocketPermission "*", "accept, connect, listen, resolve";
permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete, execute";
};
(3)启动服务器,
set CLASSPATH=%CLASSPATH%;C:\rmiexample\Server.jar
java -Djava.security.policy=C:\rmiexample\grant.txt RMIServer
(4) 启动客户端
set CLASSPATH=%CLASSPATH%;C:\rmiexample\Client.jar
java -Djava.security.policy=C:\rmiexample\grant.txt RMIClient
java security
相关推荐
在这个"rmi文件下载与上传代码"中,我们可以看到一个实现了文件管理功能的RMI系统,包括文件的列出、上传和下载。 首先,我们要理解RMI的工作原理。RMI的核心是接口,服务器端定义一个接口,客户端通过实现该接口的...
为了确保RMI的安全性,通常需要配置安全管理策略文件。例如: ```plaintext grant codeBase "file:/e:/download/rmitest/" { permission java.net.SocketPermission "*:1000-9999", "accept,connect,listen,...
这个"JAVA RMI简单例子"旨在帮助我们深入理解RMI的基本原理和实现步骤。 RMI的核心概念包括远程接口、远程对象和RMIC编译器。首先,我们需要定义一个远程接口,该接口声明了可以在远程服务器上执行的方法。这些方法...
这个“RMI入门好例子”旨在帮助初学者理解并实践RMI的基本原理和操作流程。 首先,RMI的核心思想是通过接口实现远程对象的透明调用。在示例中,`rmidemo`可能包含了服务器端的代码,它会定义一个接口,例如`...
在这个场景中,我们讨论的是如何利用RMI来实现文件或图片的上传功能,从客户端到远程服务器。 首先,我们需要理解RMI的基本原理。RMI包括三个主要组件:远程接口、远程实现和RMIServer。远程接口定义了可以在远程...
以下是一个简单的RMI文件读取服务的例子: 1. **服务端(Server)**: - 定义远程接口:创建一个名为`FileService`的接口,其中包含一个`readFile(String filePath)`方法,返回文件内容。 - 实现远程接口:创建一...
在这个例子中,我们将深入理解RMI的基本概念、工作原理以及如何创建一个完整的RMI通信系统。 首先,我们要知道RMI的核心思想是将对象的接口和其实现分开,接口在客户端,实现则在服务器端。当客户端调用接口方法时...
在"RMI例子"中,可能还包含了一个名为"logmanager"的文件或目录。这可能是用来管理日志记录的类或服务,可能与RMI服务端或客户端的日志记录功能有关。日志管理对于分布式系统来说至关重要,因为它可以帮助开发者追踪...
java rmi上传文件 在这次的项目中,对于客户端与服务器之间的通信,想了许多办法,由于做的是富客户端应用,最终将技术选定在了RMI和Java-sockets两种之间,其中RMI的灵活性不高,客户端和服务器端都必须是java编写...
Spring Remote Method Invocation (RMI) 是Java平台上的一个远程对象调用框架,它允许你在分布式环境中调用对象...通过理解并实践这个小例子,你将能够更好地掌握Spring RMI的用法,为构建更复杂的分布式系统打下基础。
在"RMI远程文件传输"这个场景中,我们主要探讨如何利用RMI技术来实现在网络环境中传输文件。 RMI的基础是Java序列化机制,它允许将Java对象转换为字节流,通过网络发送,然后在接收端恢复为原来的对象。文件传输的...
3. **Spring配置(Spring Configuration)**:在Spring配置文件中,我们需要声明远程服务bean,并配置RMI服务器和客户端的设置。例如: ```xml <bean id="rmiRegistry" class="org.springframework.remoting.rmi....
在这个"Spring RMI小例子"中,我们将深入理解Spring如何简化RMI的使用,以及如何通过它实现跨网络的交互。 首先,RMI允许Java对象在不同的JVM之间进行通信,仿佛它们都在同一个进程中运行。Spring通过提供自动注册...
在这个"SpringRMI小例子"中,我们将深入探讨如何利用Spring框架来增强RMI的功能,使其更加灵活和易于管理。 首先,我们需要理解Spring框架在RMI中的角色。Spring提供了对RMI的高级抽象,通过其`org.springframework...
根据提供的文件信息,本文将详细解释RMI(远程方法调用)的概念、工作原理以及一个简单的RMI示例。RMI是一种Java技术,允许在不同的Java虚拟机(JVM)之间进行远程过程调用。 ### RMI简介 RMI是Java平台提供的一种...
Java文件传输RMI,全称为Remote Method Invocation(远程方法调用),是Java平台提供的一种分布式计算技术。通过RMI,Java程序可以在网络上的不同计算机之间透明地调用对象的方法,仿佛这些对象都在本地一样。这一...
**Lucene 7.1 RMI远程搜索例子详解** Lucene是一个开源的全文搜索引擎库,由Apache软件基金会开发。在Lucene 7.1版本中,开发者可以利用Remote Method Invocation (RMI)技术来实现远程搜索功能,这使得在分布式环境...
在Java的远程方法调用(Remote Method Invocation, RMI)框架下,大文件传输是一个具有挑战性的任务,因为默认情况下,RMI并不直接支持大文件的高效传输。然而,通过巧妙的设计和编程,我们可以实现这样的功能。下面...
在这个"RMI远程下载文件"的例子中,我们看到一系列的类和源代码文件,它们共同构成了一个基于RMI的文件下载系统。 1. **FileInterface.class, FileInterface.java**: 这些文件定义了RMI服务的接口。`FileInterface`...
以上就是RMI客户端和服务端例子程序的基本工作原理。在实际开发中,我们还需要考虑异常处理、安全性、网络通信效率等因素。RMI是Java中构建分布式系统的一种强大工具,尤其适用于Java EE应用,例如EJB(Enterprise ...