Java Socket實戰傳輸壓縮物件
阿新 • • 發佈:2019-02-13
有些情況下比如網路環境不好或者物件比較大的情況下需要把資料物件進行壓縮然後在傳輸,此時就需要壓縮這些物件流,此時就可以GZIPInputStream和GZIPOutputStream來處理一下socket的InputStream和OutputStream。
仍然需要一個實現了java.io.Serializable介面的簡單Java物件
- package com.googlecode.garbagecan.test.socket.sample4;
- publicclass User implements java.io.Serializable {
- privatestaticfinallong 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;
- }
- publicvoid setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- publicvoid setPassword(String password) {
- this.password = password;
- }
- }
在Server端使用,socket的InputStream首先被包裝成GZIPInputStream,然後又被包裝成ObjectInputStream,而socket的OutputStream首先被包裝成GZIPOutputStream,然後又被包裝成ObjectOutputStream,如下: [java] view plaincopyprint?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; } }
- 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;
- publicclass MyServer {
- privatefinalstatic Logger logger = Logger.getLogger(MyServer.class.getName());
- publicstaticvoid main(String[] args) throws IOException {
- ServerSocket server = new ServerSocket(10000);
- while (true) {
- Socket socket = server.accept();
- socket.setSoTimeout(10 * 1000);
- invoke(socket);
- }
- }
- privatestaticvoid invoke(final Socket socket) throws IOException {
- new Thread(new Runnable() {
- publicvoid 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();
- }
- }
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,如下:
[java]
view plaincopyprint?
- 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;
- publicclass MyClient {
- privatefinalstatic Logger logger = Logger.getLogger(MyClient.class.getName());
- publicstaticvoid 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) {}
- }
- }
- }
- }
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物件例項了。