序列化和反序列化(六)——Java对象的网络传输(一)
程序员文章站
2022-06-16 16:54:18
...
Java对象网络传输常用于Java异构平台间数据的交互,Socket和HTTP均可以实现Java对象的网络传输,具体示例如下:
1、Socket实现Java对象的网络传输:
keeper Java工程:
UserInfo类:
package com.jd.vo;
import java.io.Serializable;
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) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
服务器端:
package com.jd.test;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import com.jd.vo.UserInfo;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
ObjectOutput objectOutput = new ObjectOutputStream(socket.getOutputStream());
UserInfo userInfo = new UserInfo();
userInfo.setAge(12);
userInfo.setAddress("Tom");
objectOutput.writeObject(userInfo);
objectOutput.flush();
objectOutput.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
venus Java工程:
UserInfo类: 同keeper工程UserInfo类
客户端:
package com.jd.test;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("192.168.3.55", 8080);
ObjectInput objectInput = new ObjectInputStream(socket.getInputStream());
UserInfo userInfo = (UserInfo) objectInput.readObject();
objectInput.close();
System.out.println(userInfo);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、HTTP实现Java对象的网络传输:
keeper Java Web工程:
UserInfo类:
package com.jd.vo;
import java.io.Serializable;
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) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
服务器端:
package com.jd.servlet;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jd.vo.UserInfo;
@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(response.getOutputStream());
UserInfo userInfo = new UserInfo();
userInfo.setAge(20);
userInfo.setName("Tom");
objectOutput.writeObject(userInfo);
objectOutput.flush();
objectOutput.close();
}
}
venus Java工程:
UserInfo类:同keeper工程UserInfo类
客户端:
package com.jd.test;
import java.io.ObjectInputStream;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import com.jd.vo.UserInfo;
public class Test {
public static void main(String[] args) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/keeper/DataServlet");// 创建http GET请求
CloseableHttpResponse response = httpclient.execute(httpGet);// 执行请求
if (response.getStatusLine().getStatusCode() == 200) {// 判断返回状态是否为200
HttpEntity entity = response.getEntity();
ObjectInputStream in = new ObjectInputStream(entity.getContent());
UserInfo userInfo = (UserInfo) in.readObject();
System.out.println(userInfo);
}
response.close();
httpclient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
上一篇: python-19 对象序列化
下一篇: java创建对象的五种方式