【socket】如何实现java对象的传递

问题描述

在socket客户端的设置字体事件中new了一个对象,怎么将对象传到服务端?if (e.getSource() == ft) {setfont = new SetFont(this, "字体设置", true);}怎么把setfont对象传到另一个类中(server类) 问题补充:redstarofsleep 写道

解决方案

java.io.ObjectOutputStreamAn ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconstituted on another host or in another process. Only objects that support the java.io.Serializable interface can be written to streams. The class of each serializable object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects. The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject. Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written. Primitive data types can also be written to the stream using the appropriate methods from DataOutput. Strings can also be written using the writeUTF method. The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written. For example to write an object that can be read by the example in ObjectInputStream: FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345); oos.writeObject("Today"); oos.writeObject(new Date()); oos.close(); Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures: private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException; private void writeObject(java.io.ObjectOutputStream stream) throws IOException private void readObjectNoData() throws ObjectStreamException; The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The method does not need to concern itself with the state belonging to the object's superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput. Serialization does not write out the fields of any object that does not implement the java.io.Serializable interface. Subclasses of Objects that are not serializable can be serializable. In this case the non-serializable class must have a no-arg constructor to allow its fields to be initialized. In this case it is the responsibility of the subclass to save and restore the state of the non-serializable class. It is frequently the case that the fields of that class are accessible (public, package, or protected) or that there are get and set methods that can be used to restore the state. Serialization of an object can be prevented by implementing writeObject and readObject methods that throw the NotSerializableException. The exception will be caught by the ObjectOutputStream and abort the serialization process. Implementing the Externalizable interface allows the object to assume complete control over the contents and format of the object's serialized form. The methods of the Externalizable interface, writeExternal and readExternal, are called to save and restore the objects state. When implemented by a class they can write and read their own state using all of the methods of ObjectOutput and ObjectInput. It is the responsibility of the objects to handle any versioning that occurs. Enum constants are serialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not transmitted. To serialize an enum constant, ObjectOutputStream writes the string returned by the constant's name method. Like other serializable or externalizable objects, enum constants can function as the targets of back references appearing subsequently in the serialization stream. The process by which enum constants are serialized cannot be customized; any class-specific writeObject and writeReplace methods defined by enum types are ignored during serialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored--all enum types have a fixed serialVersionUID of 0L. Primitive data, excluding serializable fields and externalizable data, is written to the ObjectOutputStream in block-data records. A block data record is composed of a header and data. The block data header consists of a marker and the number of bytes to follow the header. Consecutive primitive data writes are merged into one block-data record. The blocking factor used for a block-data record will be 1024 bytes. Each block-data record will be filled up to 1024 bytes, or be written whenever there is a termination of block-data mode. Calls to the ObjectOutputStream methods writeObject, defaultWriteObject and writeFields initially terminate any existing block-data record. Since: JDK1.1 See Also:DataOutput, ObjectInputStream, Serializable, Externalizable, Object Serialization Specification, Section 2, Object Output Classes
解决方案二:
1、使用Java序列化将对象输出到客户端,此时对象需要实现Serializable接口,或自定义了序列化功能;2、把对象序列化为XML或JSON格式传递至客户端,客户端接收到序列化后的字符串,然后再反序列化成对象即可。可以使用的三方包有:gson、fastjson、xstream等;3、使用Hessian序列化对象,传输到客户端,然后再反序列化成对象。And so on....
解决方案三:
把输入输出流封装成对象流就可以了

时间: 2024-11-18 19:05:46

【socket】如何实现java对象的传递的相关文章

传递和使用Java对象

在前例中,我们将一个字串传递给固有方法.事实上,亦可将自己创建的Java对象传递给固有方法. 在我们的固有方法内部,可访问已收到的那些对象的字段及方法. 为传递对象,声明固有方法时要采用原始的Java语法.如下例所示,MyJavaClass有一个public(公共)字段,以及一个public方法.UseObjects类声明了一个固有方法,用于接收MyJavaClass类的一个对象.为调查固有方法是否能控制自己的自变量,我们设置了自变量的public字段,调用固有方法,然后打印出public字段的

java对象序列化学习笔记

java对象|笔记 目前网络上关于对象序列化的文章不少,但是我发现详细叙述用法和原理的文章太少.本人把自己经过经验总结和实际运用中的体会写成的学习笔记贡献给大家.希望能为整个java社区的繁荣做一点事情.    序列化的过程就是对象写入字节流和从字节流中读取对象.将对象状态转换成字节流之后,可以用java.io包中的各种字节流类将其保存到文件中,管道到另一线程中或通过网络连接将对象数据发送到另一主机.对象序列化功能非常简单.强大,在RMI.Socket.JMS.EJB都有应用.对象序列化问题在网

Java对象序列化使用基础

所谓对象序列化就是将对象的状态转换成字节流,以后可以通过这些值再生成相同状态的对象.这个过程也可以通过网络实现,可以先在Windows机器上创建一个对象,对其序列化,然后通过网络发给一台Unix机器,然后在那里准确无误地重新"装配".像RMI.Socket.JMS.EJB它们中的一种,彼此为什么能够传递Java对象,当然都是对象序列化机制的功劳. Java对象序列化机制一般来讲有两种用途: Java的JavaBeans: Bean的状态信息通常是在设计时配置的,Bean的状态信息必须被

Ajax 的 Java 对象序列化

ajax|对象 如果您正在使用异步 JavaScript 和 XML(Ajax)进行 Java Web 开发,那么您最关心的问题可能就是把数据从服务器传递给客户机.在面向 Java 开发人员的 Ajax 系列的文章中,Philip McCarthy 介绍了 Java 对象序列化的五种方式,并提供了选择最适合应用程序的数据格式和技术所需要的全部信息.本文将侧重于许多 Java Web 开发人员最关心的问题:为客户机生成数据. 多数 Java 开发人员已经把模型-视图-控制器(MVC)模式应用在他们

Ajax 的 Java 对象序列化(1)

ajax|对象 如果您正在使用异步 JavaScript 和 XML(Ajax)进行 Java Web 开发,那么您最关心的问题可能就是把数据从服务器传递给客户机. 在面向 Java 开发人员的 Ajax 系列的文章中,Philip McCarthy 介绍了 Java 对象序列化的五种方式,并提供了选择最适合应用程序的数据格式和技术所需要的全部信息.本文将侧重于许多 Java Web 开发人员最关心的问题:为客户机生成数据. 多数 Java 开发人员已经把模型-视图-控制器(MVC)模式应用在他

面向Java开发人员的Ajax:Java对象序列化(1)

ajax|java对象 本文我们讨论 Ajax 开发的基础知识,但是将侧重于许多 Java Web 开发人员最关心的问题:为客户机生成数据. 多数 Java 开发人员已经把模型-视图-控制器(MVC)模式应用在他们的 Web 应用程序上.在传统的 Web 应用程序中,视图组件由 JSP 或者其他表示技术(例如 Velocity 模板)构成. 这些表示组件动态地生成全新的 HTML 页面,替代用户以前正在查看的页面,从而更新用户界面.但是,在 Java Web 应用程序使用 Ajax UI 的情况

面向Java开发人员的Ajax: Ajax的Java对象序列化

在这个系列的 第一篇文章 中,我介绍了 Ajax 的构造块: 如何用 JavaScript XMLHttpRequest 对象从 Web 页面向服务器发送异步请求. 如何用 Java servlet 处理和响应请求(向客户机返回 XML 文档). 如何在客户端用响应文档更新页面视图. 这一次,我将继续讨论 Ajax 开发的基础知识,但是将侧重于许多 Java Web 开发人员最关心的问题:为客户机生成数据. 多数 Java 开发人员已经把模型-视图-控制器(MVC)模式应用在他们的 Web 应用

浅谈Socket编程及Java实现

Java是一种可用于进行网络编程的语言,它提供了两种功能强大的网络支持机制:URL访问网络资源的类和用Socket通讯的类,来满足不同的要求.一是URL用于访问Internet网上资源的应用:另一种是针对client/server(客户端/服务器)模式的应用以及实现某些特殊的协议的应用,它的通讯过程是基于TCP/IP协议中传输层接口socket实现的.本文想简单的介绍一下Socket编程的Java实现方法. 客户基于服务器之间使用的大部分通讯组件都是基于socket接口来实现的.Socket是两

java 参数的传递 更新数据

问题描述 java 参数的传递 更新数据 有一个程序,数据的更新需要传递参数,但是再当前页面无法传递参数,我要怎么实现参数的传递 解决方案 不知道你说的当前页面无法传递参数是什么意思,传不过去参数,会不会因为form没有设置method="POST"或者是GET,又或者你使用了ajax没有配置正确. java中前台有request,application,session,cookie等四大页面对象,有时也会通过隐藏的input标签(hidden属性)存储值 解决方案二: 不能传递参数,