新聞中心
上一篇文章說(shuō)到了用Java Socket來(lái)傳輸對(duì)象,但是在有些情況下比如網(wǎng)絡(luò)環(huán)境不好或者對(duì)象比較大的情況下需要把數(shù)據(jù)對(duì)象進(jìn)行壓縮然后在傳輸,此時(shí)就需要壓縮這些對(duì)象流,此時(shí)就可以GZIPInputStream和GZIPOutputStream來(lái)處理一下socket的InputStream和OutputStream。

創(chuàng)新互聯(lián)建站長(zhǎng)期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為吉安企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,吉安網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
仍然需要一個(gè)實(shí)現(xiàn)了java.io.Serializable接口的簡(jiǎn)單Java對(duì)象:
- 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);
- }
- try {
- oos.close();
- } catch (IOException e) {
- }
- try {
- ois.close();
- } catch (IOException e) {
- }
- try {
- socket.close();
- } catch (IOException e) {
- }
- }
- }
- }
***測(cè)試上面的代碼,首先運(yùn)行Server類,然后運(yùn)行Client類,就可以分別在Server端和Client端控制臺(tái)看到接收到的User對(duì)象實(shí)例了。
原文鏈接:http://blog.csdn.net/kongxx/article/details/7259834
網(wǎng)頁(yè)名稱:JavaSocket實(shí)戰(zhàn)之四:傳輸壓縮對(duì)象
瀏覽地址:http://fisionsoft.com.cn/article/dpdpdsi.html


咨詢
建站咨詢
