java网络编程(四)压缩对象传输


对象的压缩存储主要是采用的是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) {} 
                 } 
             } 
         } 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值