对象的压缩存储主要是采用的是GZIPInputStream以及相对应的GZIPOutputStream来实现的,将其中的对象进行压缩后发送到相应的客户端上,代码如下:
package com.sun.net.gzip;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* 类名: GzipServer
* 包名: com.sun.net.gzip
* 作者: Administrator
* 时间: 2015年1月26日 下午3:58:40
* 描述:
*/
public class GzipServer {
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) {
ex.printStackTrace();
} catch(ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
try {
ois.close();
} catch(Exception ex) {}
try {
oos.close();
} catch(Exception ex) {}
try {
socket.close();
} catch(Exception ex) {}
}
}
}).start();
}
}
客户端程序代码如下:
package com.sun.net.gzip;
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.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* 类名: GzipClient
* 包名: com.sun.net.gzip
* 作者: Administrator
* 时间: 2015年1月26日 下午4:00:38
* 描述:
*/
public class GzipClient {
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) {
ex.printStackTrace();
} finally {
try {
ois.close();
} catch(Exception ex) {}
try {
oos.close();
} catch(Exception ex) {}
try {
socket.close();
} catch(Exception ex) {}
}
}
}
}