Java Socket实现传输压缩对象的方法示例
作者:kongxx 时间:2022-12-18 06:52:30
本文实例讲述了Java Socket实现传输压缩对象的方法。分享给大家供大家参考,具体如下:
前面文章《Java Socket实现的传输对象功能示例》说到了用Java Socket来传输对象,但是在有些情况下比如网络环境不好或者对象比较大的情况下需要把数据对象进行压缩然后在传输,此时就需要压缩这些对象流,此时就可以GZIPInputStream和GZIPOutputStream来处理一下socket的InputStream和OutputStream。
仍然需要一个实现了java.io.Serializable接口的简单Java对象
package com.googlecode.garbagecan.test.socket.sample4;
public class User implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在Server端使用,socket的InputStream首先被包装成GZIPInputStream,然后又被包装成ObjectInputStream,而socket的OutputStream首先被包装成GZIPOutputStream,然后又被包装成ObjectOutputStream,如下:
package com.googlecode.garbagecan.test.socket.sample4;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class MyServer {
private final static Logger logger = Logger.getLogger(MyServer.class.getName());
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(10000);
while (true) {
Socket socket = server.accept();
socket.setSoTimeout(10 * 1000);
invoke(socket);
}
}
private static void invoke(final Socket socket) throws IOException {
new Thread(new Runnable() {
public void run() {
GZIPInputStream gzipis = null;
ObjectInputStream ois = null;
GZIPOutputStream gzipos = null;
ObjectOutputStream oos = null;
try {
gzipis = new GZIPInputStream(socket.getInputStream());
ois = new ObjectInputStream(gzipis);
gzipos = new GZIPOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(gzipos);
Object obj = ois.readObject();
User user = (User)obj;
System.out.println("user: " + user.getName() + "/" + user.getPassword());
user.setName(user.getName() + "_new");
user.setPassword(user.getPassword() + "_new");
oos.writeObject(user);
oos.flush();
gzipos.finish();
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
} catch(ClassNotFoundException ex) {
logger.log(Level.SEVERE, null, ex);
} finally {
try {
ois.close();
} catch(Exception ex) {}
try {
oos.close();
} catch(Exception ex) {}
try {
socket.close();
} catch(Exception ex) {}
}
}
}).start();
}
}
Client也和Server端类似,同样要不socket的XXXStream包装成GZIPXXXStream,然后再包装成ObjectXXXStream,如下:
package com.googlecode.garbagecan.test.socket.sample4;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class MyClient {
private final static Logger logger = Logger.getLogger(MyClient.class.getName());
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
Socket socket = null;
GZIPOutputStream gzipos = null;
ObjectOutputStream oos = null;
GZIPInputStream gzipis = null;
ObjectInputStream ois = null;
try {
socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress("localhost", 10000);
socket.connect(socketAddress, 10 * 1000);
socket.setSoTimeout(10 * 1000);
gzipos = new GZIPOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(gzipos);
User user = new User("user_" + i, "password_" + i);
oos.writeObject(user);
oos.flush();
gzipos.finish();
gzipis = new GZIPInputStream(socket.getInputStream());
ois = new ObjectInputStream(gzipis);
Object obj = ois.readObject();
if (obj != null) {
user = (User)obj;
System.out.println("user: " + user.getName() + "/" + user.getPassword());
}
} catch(IOException ex) {
logger.log(Level.SEVERE, null, ex);
} finally {
try {
ois.close();
} catch(Exception ex) {}
try {
oos.close();
} catch(Exception ex) {}
try {
socket.close();
} catch(Exception ex) {}
}
}
}
}
最后测试上面的代码,首先运行Server类,然后运行Client类,就可以分别在Server端和Client端控制台看到接收到的User对象实例了。
希望本文所述对大家java程序设计有所帮助。
标签:Java,Socket
0
投稿
猜你喜欢
C#多线程编程Task用法详解
2021-10-27 14:26:10
SpringBoot生产环境和测试环境配置分离的教程详解
2022-04-03 22:09:47
flutter实现更新弹窗内容例子(亲测有效)
2023-10-09 03:25:36
Java基础-Java的体系结构
2022-05-18 00:19:05
关于EntityWrapper的in用法
2023-11-29 09:02:11
Android桌面插件App Widget用法分析
2022-02-05 02:26:39
Java如何有效避免SQL注入漏洞的方法总结
2021-11-22 07:01:54
java中switch选择语句代码详解
2023-09-28 19:34:29
C#探秘系列(三)——StackTrace,Trim
2022-04-27 21:24:03
java生成随机字符串的两种方法
2022-09-09 22:49:26
IntelliJ IDEA像Eclipse一样打开多个项目的图文教程
2022-03-14 05:31:40
c# List find()方法返回值的问题说明(返回结果为对象的指针)
2023-11-20 21:55:34
利用Distinct()内置方法对List集合的去重问题详解
2023-01-31 00:45:30
剑指Offer之Java算法习题精讲链表专项训练
2023-11-29 16:31:48
Java使用JDBC连接Oracle_MSSQL实例代码
2023-04-19 19:34:46
C#算法之各位相加
2021-09-03 17:32:42
android实现录屏功能
2022-04-18 05:11:22
Java多线程ThreadPoolExecutor详解
2023-11-23 18:39:32
Android 用 camera2 API 自定义相机
2023-04-30 11:29:37
Redis原子计数器incr,防止并发请求操作
2021-07-03 13:35:15