转自:http://developer.51cto.com/art/201202/317546.htm

上一篇文章说到了用Java Socket来传输对象,但是在有些情况下比如网络环境不好或者对象比较大的情况下需要把数据对象进行压缩然后在传输,此时就需要压缩这些对象流,此时就可以GZIPInputStream和GZIPOutputStream来处理一下socket的InputStream和OutputStream。

仍然需要一个实现了java.io.Serializable接口的简单Java对象:

 1 package com.googlecode.garbagecan.test.socket.sample4;  
 2  
 3 public class User implements java.io.Serializable {  
 4     private static final long serialVersionUID = 1L;  
 5     private String name;  
 6     private String password;  
 7  
 8     public User() {  
 9           
10     }  
11       
12     public User(String name, String password) {  
13         this.name = name;  
14         this.password = password;  
15     }  
16       
17     public String getName() {  
18         return name;  
19     }  
20  
21     public void setName(String name) {  
22         this.name = name;  
23     }  
24  
25     public String getPassword() {  
26         return password;  
27     }  
28  
29     public void setPassword(String password) {  
30         this.password = password;  
31     }  
32       
33 } 

在Server端使用,socket的InputStream首先被包装成GZIPInputStream,然后又被包装成ObjectInputStream,而socket的OutputStream首先被包装成GZIPOutputStream,然后又被包装成ObjectOutputStream,如下:

 1 package com.googlecode.garbagecan.test.socket.sample4;  
 2  
 3 import java.io.IOException;  
 4 import java.io.ObjectInputStream;  
 5 import java.io.ObjectOutputStream;  
 6 import java.net.ServerSocket;  
 7 import java.net.Socket;  
 8 import java.util.logging.Level;  
 9 import java.util.logging.Logger;  
10 import java.util.zip.GZIPInputStream;  
11 import java.util.zip.GZIPOutputStream;  
12  
13 public class MyServer {  
14  
15     private final static Logger logger = Logger.getLogger(MyServer.class.getName());  
16       
17     public static void main(String[] args) throws IOException {  
18         ServerSocket server = new ServerSocket(10000);  
19  
20         while (true) {  
21             Socket socket = server.accept();  
22             socket.setSoTimeout(10 * 1000);  
23             invoke(socket);  
24         }  
25     }  
26  
27     private static void invoke(final Socket socket) throws IOException {  
28         new Thread(new Runnable() {  
29             public void run() {  
30                 GZIPInputStream gzipis = null;  
31                 ObjectInputStream ois = null;  
32                 GZIPOutputStream gzipos = null;  
33                 ObjectOutputStream oos = null;  
34                   
35                 try {  
36                     gzipis = new GZIPInputStream(socket.getInputStream());  
37                     ois = new ObjectInputStream(gzipis);  
38                     gzipos = new GZIPOutputStream(socket.getOutputStream());  
39                     oos = new ObjectOutputStream(gzipos);  
40  
41                     Object obj = ois.readObject();  
42                     User user = (User)obj;  
43                     System.out.println("user: " + user.getName() + "/" + user.getPassword());  
44  
45                     user.setName(user.getName() + "_new");  
46                     user.setPassword(user.getPassword() + "_new");  
47  
48                     oos.writeObject(user);  
49                     oos.flush();  
50                     gzipos.finish();  
51                 } catch (IOException ex) {  
52                     logger.log(Level.SEVERE, null, ex);  
53                 } catch(ClassNotFoundException ex) {  
54                     logger.log(Level.SEVERE, null, ex);  
55                 } finally {  
56                     try {  
57                         ois.close();  
58                     } catch(Exception ex) {}  
59                     try {  
60                         oos.close();  
61                     } catch(Exception ex) {}  
62                     try {  
63                         socket.close();  
64                     } catch(Exception ex) {}  
65                 }  
66             }  
67         }).start();  
68     }  
69 } 

Client也和Server端类似,同样要不socket的XXXStream包装成GZIPXXXStream,然后再包装成ObjectXXXStream,如下:

 1 package com.googlecode.garbagecan.test.socket.sample4;  
 2  
 3 import java.io.IOException;  
 4 import java.io.ObjectInputStream;  
 5 import java.io.ObjectOutputStream;  
 6 import java.net.InetSocketAddress;  
 7 import java.net.Socket;  
 8 import java.net.SocketAddress;  
 9 import java.util.logging.Level;  
10 import java.util.logging.Logger;  
11 import java.util.zip.GZIPInputStream;  
12 import java.util.zip.GZIPOutputStream;  
13  
14 public class MyClient {  
15       
16     private final static Logger logger = Logger.getLogger(MyClient.class.getName());  
17       
18     public static void main(String[] args) throws Exception {  
19         for (int i = 0; i < 10; i++) {  
20             Socket socket = null;  
21             GZIPOutputStream gzipos = null;  
22             ObjectOutputStream oos = null;  
23             GZIPInputStream gzipis = null;  
24             ObjectInputStream ois = null;  
25               
26             try {  
27                 socket = new Socket();  
28                 SocketAddress socketAddress = new InetSocketAddress("localhost", 10000);   
29                 socket.connect(socketAddress, 10 * 1000);  
30                 socket.setSoTimeout(10 * 1000);  
31                   
32                 gzipos = new GZIPOutputStream(socket.getOutputStream());  
33                 oos = new ObjectOutputStream(gzipos);  
34                 User user = new User("user_" + i, "password_" + i);  
35                 oos.writeObject(user);  
36                 oos.flush();  
37                 gzipos.finish();  
38                   
39                 gzipis = new GZIPInputStream(socket.getInputStream());  
40                 ois = new ObjectInputStream(gzipis);  
41                 Object obj = ois.readObject();  
42                 if (obj != null) {  
43                     user = (User)obj;  
44                     System.out.println("user: " + user.getName() + "/" + user.getPassword());  
45                 }  
46             } catch(IOException ex) {  
47                 logger.log(Level.SEVERE, null, ex);  
48             }  
49             try {  
50                 oos.close();  
51             } catch (IOException e) {  
52             }  
53             try {  
54                 ois.close();  
55             } catch (IOException e) {  
56             }  
57             try {  
58                 socket.close();  
59             } catch (IOException e) {  
60             }  
61         }  
62     }  
63 } 

最后测试上面的代码,首先运行Server类,然后运行Client类,就可以分别在Server端和Client端控制台看到接收到的User对象实例了。

 

  1. package com.googlecode.garbagecan.test.socket.sample4;  
  2.  
  3. public class User implements java.io.Serializable {  
  4.     private static final long serialVersionUID = 1L;  
  5.     private String name;  
  6.     private String password;  
  7.  
  8.     public User() {  
  9.           
  10.     }  
  11.       
  12.     public User(String name, String password) {  
  13.         this.name = name;  
  14.         this.password = password;  
  15.     }  
  16.       
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.  
  25.     public String getPassword() {  
  26.         return password;  
  27.     }  
  28.  
  29.     public void setPassword(String password) {  
  30.         this.password = password;  
  31.     }  
  32.       
posted on 2018-12-02 21:33  Sharpest  阅读(1106)  评论(0编辑  收藏  举报