這篇文章將為大家詳細講解有關(guān)Java Socket中怎樣傳輸壓縮對象,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)介紹好的網(wǎng)站是理念、設(shè)計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)公司擁有的網(wǎng)站設(shè)計理念、多方位的設(shè)計風格、經(jīng)驗豐富的設(shè)計團隊。提供PC端+手機端網(wǎng)站建設(shè),用營銷思維進行網(wǎng)站設(shè)計、采用先進技術(shù)開源代碼、注重用戶體驗與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。
上一篇文章說到了用Java Socket來傳輸對象,但是在有些情況下比如網(wǎng)絡(luò)環(huán)境不好或者對象比較大的情況下需要把數(shù)據(jù)對象進行壓縮然后在傳輸,此時就需要壓縮這些對象流,此時就可以GZIPInputStream和GZIPOutputStream來處理一下socket的InputStream和OutputStream。
仍然需要一個實現(xiàn)了java.io.Serializable接口的簡單Java對象:
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) { } } } }
測試上面的代碼,首先運行Server類,然后運行Client類,就可以分別在Server端和Client端控制臺看到接收到的User對象實例了。
關(guān)于Java Socket中怎樣傳輸壓縮對象就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。