序列化和反序列化(六)——Java对象的网络传输(一)

时间:2025-03-27 17:06:38

Java对象网络传输常用于Java异构平台间数据的交互,SocketHTTP均可以实现Java对象的网络传输,具体示例如下:

1Socket实现Java对象的网络传输

      keeper Java工程:

             UserInfo类:

package ;

import ;

public class UserInfo implements Serializable{

	private static final long serialVersionUID = 3103317843148814898L;
	private int age;
	private String name;

	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		 = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		 = name;
	}
}

             服务器端:

package ;

import ;
import ;
import ;
import ;

import ;

public class Server {

	public static void main(String[] args) {
		try {
			ServerSocket serverSocket = new ServerSocket(8080);
			Socket socket = ();
			ObjectOutput objectOutput = new ObjectOutputStream(());
			
			UserInfo userInfo = new UserInfo();
			(12);
			("Tom");
			
			(userInfo);
			();
			();
			();
		} catch (Exception e) {
			();
		}
	}
}

      venus Java工程:

             UserInfo类:   keeper工程UserInfo

             客户端:

package ;

import ;
import ;
import ;

public class Client {

	public static void main(String[] args) {
		try {
			Socket socket = new Socket("192.168.3.55", 8080);
			ObjectInput objectInput = new ObjectInputStream(());
			UserInfo userInfo = (UserInfo) ();
			();
			(userInfo);
			();
		} catch (Exception e) {
			();
		}
	}
}

2HTTP实现Java对象的网络传输

      keeper Java Web工程:

             UserInfo类:

package ;

import ;

public class UserInfo implements Serializable{

	private static final long serialVersionUID = 3103317843148814898L;
	private int age;
	private String name;

	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		 = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		 = name;
	}
}

             服务器端:

package ;

import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;

import ;

@WebServlet("/DataServlet")
public class DataServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ObjectOutput objectOutput = new ObjectOutputStream(());
		
		UserInfo userInfo = new UserInfo();
		(20);
		("Tom");
		
		(userInfo);
		();
		();
	}
}

      venus Java工程:

             UserInfo类:同keeper工程UserInfo

             客户端:

package ;

import ;

import ;
import ;
import ;
import ;
import ;

import ;

public class Test {

	public static void main(String[] args) {
		try {
			CloseableHttpClient httpclient = ();
			HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/keeper/DataServlet");// 创建http GET请求
			CloseableHttpResponse response = (httpGet);// 执行请求
			if (().getStatusCode() == 200) {// 判断返回状态是否为200
				HttpEntity entity = ();
				ObjectInputStream in = new ObjectInputStream(());
				UserInfo userInfo = (UserInfo) ();
				(userInfo);
			}
			();
			();
		} catch (Exception e) {
			();
		}
	}
}