通过 Internet 发送对象
我定义一个类,然后设置该类类型的一个对象。我想透明地将这个对象发送到另一台计算机上运行的另一个Java应用程序。实现这一目标的最佳技术是什么?
I define a class, and then I instate an object of that class type. I want to send this object to another Java application running on a different computer transparently. What is the best technology to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要首先研究 Java 的序列化可序列化接口。 Sun 有一篇关于它的好文章,名为探索 Java 序列化 API 的秘密 。
有关实际传输序列化对象的信息,请参阅 Java Sockets 教程通过网络。
You'll want to start by looking into serialization with the Java Serializable interface. Sun has a good article on it called Discover the secrets of the Java Serialization API.
Refer to the Java Sockets tutorial for information on actually transferring the serialized object over the network.
您可以使用 java API 创建对象流并发送任何可序列化的对象。但您必须注意,这些信息在网络中是未加密的:
在发送方:
以及在接收端:
you can create object streams using the java API and send any serializable object. but you'll have to mind that these go unencrypted through the network:
on the sender's side:
and on the receiving end:
有很多方法可以做到这一点。以下是一些需要研究的事项,您可以选择最适合您的应用程序的一项。
推送位几乎任何通信框架都允许您以一种或另一种方式通过网络推送对象。您只需查看它们并看看哪一个适合您的应用程序。快速谷歌应该会找到更多方法。
There are a lot of ways to do this. Here are some things to look into and you can pick the one that would work best for your application.
Pretty much any communication framework will allow you to push objects over a network in one way or another. You just need to review them and see which works for your application. A quick google should find even more methods.
实现这一点的(事实上的)标准是使用网络服务,例如使用JAX-WS 捆绑在 Java 6 中。请参阅 本教程适用于 java-first 示例(即使用注释)。这非常简单且直接。
还有其他方法,例如
序列化
Socket、RMI、EJB,但是,当通过 Internet 工作时,Web 服务是一种自然的选择,因为它们依赖于现有标准(SOAP、HTTP)并且可以轻松处理防火墙(这对于所有其他解决方案来说可能是真正的问题)。A (de facto) standard to implement this would be to use a web service, for example using JAX-WS which is bundled in Java 6. See this tutorial for a java-first sample (i.e. using annotations). This is pretty straight forward and easy.
There are other approaches such as
Serialization
over a Socket, RMI, EJBs but, when working over the Internet, web services are a kind of natural choice as they rely on existing standards (SOAP, HTTP) and deal easily with firewalls (which might be a real issue for all other solutions).Java 使用 ObjectOutputStream(和 ObjectInputStream)提供(二进制)对象序列化。您只需将 writeObject() 写入流中,然后在另一端使用 readObject() 即可。为此,您所需要做的就是实现 Serialized 接口。
但您可能有兴趣将其提升一级并使用远程方法调用,而不是手动执行此操作。通过 RMI,您可以调用位于另一个 JVM 中的对象的方法,并且所有序列化和网络都在幕后进行。
为了完整起见,如果不能使用二进制格式,还可以使用 XML bean 序列化。该 XML 格式非常通用(即:冗长且丑陋),但有一些流行的库(如 XStream)可以创建替代的 XML 序列化。
Java provides (binary) object serialization using the ObjectOutputStream (and ObjectInputStream). You can just writeObject() into the stream and readObject() on the other end. All you need to do for this to work is implement the Serializable interface.
But rather than doing that manually, you may be interested in taken it one level up and using Remote Method Invocation. With RMI you can call methods on objects that live in another JVM, and all the serialization and networking happens under the hood.
And for the sake of completeness, there is also XML bean serialization, if you cannot use the binary format. That XML format is very generic (read: verbose and ugly), but there are some popular libraries (like XStream) that create alternative XML serializations.