java 通过 Internet 发送对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1931466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:34:21  来源:igfitidea点击:

Sending an object over the Internet

javanetworkingserialization

提问by joemoe

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?

我定义了一个类,然后我设置了一个该类类型的对象。我想将此对象透明地发送到在另一台计算机上运行的另一个 Java 应用程序。实现这一目标的最佳技术是什么?

回答by Bill the Lizard

You'll want to start by looking into serialization with the Java Serializableinterface. Sun has a good article on it called Discover the secrets of the Java Serialization API.

您首先需要研究使用 Java Serializable接口进行序列化。Sun 有一篇关于它的好文章,称为Discover the secrets of the Java Serialization API

Refer to the Java Sockets tutorialfor information on actually transferring the serialized object over the network.

有关通过网络实际传输序列化对象的信息,请参阅Java 套接字教程

回答by fasseg

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:

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

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();

回答by Rodney S. Foley

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
  • J2EE
  • 风险管理信息
  • 对象序列化通过套接字推送位
  • 网页服务

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.

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

回答by Pascal Thivent

A (de facto) standard to implement this would be to use a web service, for example using JAX-WSwhich is bundled in Java 6. See this tutorialfor a java-first sample (i.e. using annotations). This is pretty straight forward and easy.

实现这一点的(事实上的)标准是使用Web 服务,例如使用捆绑在 Java 6 中的JAX-WS。有关Java 优先示例(即使用注释),请参阅本教程。这非常简单直接。

There are other approaches such as Serializationover 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 issuefor all other solutions).

还有其他的方法,如Serialization通过套接字,RMI,EJB的,但工作在互联网上时,Web服务是一种自然的选择,因为他们依赖于现有的标准(SOAP,HTTP)和与防火墙轻松应对(这可能是一个所有其他解决方案的实际问题)。

回答by Prashant Gautam

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.

Java 使用 ObjectOutputStream(和 ObjectInputStream)提供(二进制)对象序列化。您可以将 writeObject() 写入流并在另一端 readObject() 。为此,您需要做的就是实现 Serializable 接口。

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.

但不是手动执行此操作,您可能有兴趣将其提升一级并使用远程方法调用。使用 RMI,您可以调用位于另一个 JVM 中的对象上的方法,并且所有序列化和网络都在幕后进行。

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.

并且为了完整起见,如果您不能使用二进制格式,还有 XML bean 序列化。这种 XML 格式非常通用(阅读:冗长和丑陋),但有一些流行的库(如 XStream)可以创建替代的 XML 序列化。