通过 Internet 发送对象

发布于 2024-08-16 06:07:09 字数 78 浏览 5 评论 0原文

我定义一个类,然后设置该类类型的一个对象。我想透明地将这个对象发送到另一台计算机上运行的另一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅黛梨妆こ 2024-08-23 06:07:09

您需要首先研究 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.

夜灵血窟げ 2024-08-23 06:07:09

您可以使用 java API 创建对象流并发送任何可序列化的对象。但您必须注意,这些信息在网络中是未加密的:

在发送方:

CustomObject objectToSend=new CustomObject();
Socket s = new Socket("yourhostname", 1234);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(objectToSend);
out.flush();

以及在接收端:

ServerSocket server = new ServerSocket(1234);
Socket s = server.accept();
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
CustomObject objectReceived = (CustomObject) in.readObject();

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:

CustomObject objectToSend=new CustomObject();
Socket s = new Socket("yourhostname", 1234);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(objectToSend);
out.flush();

and on the receiving end:

ServerSocket server = new ServerSocket(1234);
Socket s = server.accept();
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
CustomObject objectReceived = (CustomObject) in.readObject();
路弥 2024-08-23 06:07:09

有很多方法可以做到这一点。以下是一些需要研究的事项,您可以选择最适合您的应用程序的一项。

  • J2EE
  • RMI
  • 对象序列化通过套接字
  • Web 服务

推送位几乎任何通信框架都允许您以一种或另一种方式通过网络推送对象。您只需查看它们并看看哪一个适合您的应用程序。快速谷歌应该会找到更多方法。

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.

  • J2EE
  • RMI
  • Object Serialization pushing the bits over a Socket
  • Webservices

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.

甩你一脸翔 2024-08-23 06:07:09

实现这一点的(事实上的)标准是使用网络服务,例如使用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).

走走停停 2024-08-23 06:07:09

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文