日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開(kāi)發(fā)教程 > 詳細(xì)頁(yè)面

運(yùn)用Java完成網(wǎng)絡(luò)傳輸數(shù)據(jù)的壓縮

運(yùn)用Java完成網(wǎng)絡(luò)傳輸數(shù)據(jù)的壓縮

更新時(shí)間:2022-05-05 文章作者:未知 信息來(lái)源:網(wǎng)絡(luò) 閱讀次數(shù):

 Q: 本周,我回答了兩個(gè)使用Java進(jìn)行數(shù)據(jù)壓縮的問(wèn)題.

第一個(gè)問(wèn)題是: 我怎樣才能壓縮那些不在文件中的數(shù)據(jù).

第二個(gè)問(wèn)題是: 我以極大的熱情閱讀了Todd Sundsted的"壓縮你的數(shù)據(jù),從而提高你的網(wǎng)絡(luò)應(yīng)用程序的性能",但是讀完后我卻有點(diǎn)失望.當(dāng)我讀到文章標(biāo)題時(shí)我很高興.我想我總算找到了解決問(wèn)題的辦法了.

    在我們的公司,我們?cè)噲D提高一個(gè)組織數(shù)據(jù)的RMI應(yīng)用程序的性能.服務(wù)器端進(jìn)行了絕大部分的處理和優(yōu)化.我們花了一年半的時(shí)間去提高性能,但是現(xiàn)在看來(lái)瓶頸在于數(shù)據(jù)的傳輸上.在一天的任何時(shí)間內(nèi),我們都有可能在客戶和服務(wù)器之間傳送成千上萬(wàn)的數(shù)據(jù).

      一種可能的解決辦法,我建議我們能夠在把數(shù)據(jù)返回給客戶端時(shí)先壓縮這些數(shù)據(jù),這在Todd的文章中已經(jīng)說(shuō)得很清楚了.但是,文章中的例子卻是壓縮文件,而不是我們所需要的----對(duì)數(shù)據(jù)進(jìn)行壓縮.

    在RMI中的實(shí)現(xiàn)中,我們先從數(shù)據(jù)庫(kù)取得數(shù)據(jù),再把數(shù)據(jù)放入一個(gè)列表中,接著把這個(gè)列表返回給客戶端,最后再把它們插入JTable中.我想在把數(shù)據(jù)返回給客戶時(shí),首先把列表中的數(shù)據(jù)壓縮,然后在客戶端解壓縮,最后把數(shù)據(jù)插入到表格中.

這樣的想法可行嗎?

A:最近我收到了一些關(guān)于Todd的文章的疑問(wèn).很多讀者看起來(lái)對(duì)文章中的舉例很疑惑.因?yàn)槲恼轮械睦邮且晕募䦃嚎s為核心的.

   首先回答第一個(gè)問(wèn)題,當(dāng)你使用ZipInputStream 和 ZipOutputStream 并沒(méi)有強(qiáng)制你必須使用文件.唯一要注意的是你必須把數(shù)據(jù)轉(zhuǎn)換為字節(jié)數(shù)組的形式.

  第二個(gè)問(wèn)題比較棘手.在網(wǎng)絡(luò)中,以RMI方式通信就需要作一些調(diào)整了.為了在傳送數(shù)據(jù)之前就讓RMI進(jìn)行數(shù)據(jù)壓縮,你必須創(chuàng)建一個(gè)能夠壓縮數(shù)據(jù)的新的套接字.然后,當(dāng)你創(chuàng)建了一個(gè)套接字后,你得告訴RMI使用這一套接字.

  以下是創(chuàng)建一個(gè)RMI形式的套接字的簡(jiǎn)要步驟:

  1:選擇或者創(chuàng)建一個(gè)新的套接字.(可以參看SUN'S的"創(chuàng)建一個(gè)典型的套接字").

  2:創(chuàng)建一個(gè)服務(wù)器端的套接字.

  3:創(chuàng)建一個(gè)RMIClientSocketFactory

  4:創(chuàng)建一個(gè)RMIServerSocketFactory

  5:創(chuàng)建一個(gè)繼承了UnicastRemoteObjec的遠(yuǎn)程對(duì)象,從而使用新的factories.

  根據(jù)這一大致的想法,我們來(lái)看每一步如何具體的實(shí)現(xiàn).

步驟1: 創(chuàng)建ZipSocket

由于要進(jìn)行Zip壓縮,我們重新創(chuàng)建這樣的套接字

mport java.io.InputStream;

import java.io.OutputStream;

import java.util.zip.ZipInputStream;

import java.util.zip.ZipOutputStream;

import java.net.Socket;

public class ZipSocket extends Socket {



    private InputStream in;

    private OutputStream out;

    

    public ZipSocket() { super(); }



    public ZipSocket(String host, int port)

        throws IOException {

            super(host, port);

    }

    

    public InputStream getInputStream()

        throws IOException {

            if (in == null) {

                in = new ZipInputStream(super.getInputStream());

            }

       return in;

    }



    public OutputStream getOutputStream()

        throws IOException {

            if (out == null) {

                out = new ZipOutputStream(super.getOutputStream());

            }

            return out;

    }

    

    public synchronized void close() throws IOException {

        OutputStream o = getOutputStream();

        o.flush();

        super.close();

    }

}





步驟2: 創(chuàng)建ZipServerSocket



import java.net.ServerSocket;

import java.net.Socket;

import java.io.IOException;



public class ZipServerSocket extends ServerSocket

{

  public ZipServerSocket(int port) throws IOException {    

    super(port);

  }

  

  public Socket accept() throws IOException {

    Socket socket = new ZipSocket();

    implAccept(socket);

    return socket;

  }

}





步驟3:創(chuàng)建ZipClientSocketFactory

客戶端的factory的創(chuàng)建必須遵循以下的形式:



import java.io.IOException;

import java.io.Serializable;

import java.net.Socket;

import java.rmi.server.RMIClientSocketFactory;



public class ZipClientSocketFactory

    implements RMIClientSocketFactory, Serializable {

    public Socket createSocket(String host, int port)

        throws IOException {

            ZipSocket socket = new ZipSocket(host, port);

            return socket;

    }

}



步驟4:創(chuàng)建ZipServerSocketFactory



import java.io.IOException;

import java.io.Serializable;

import java.net.ServerSocket;

import java.rmi.server.RMIServerSocketFactory;

  

public class ZipServerSocketFactory

    implements RMIServerSocketFactory, Serializable {



    public ServerSocket createServerSocket(int port)

        throws IOException {

            ZipServerSocket server = new ZipServerSocket(port);

            return server;

    }

}



步驟5: 創(chuàng)建一個(gè)繼承了UnicastRemoteObjec的遠(yuǎn)程對(duì)象,從而使用新的factories.

public class YourRMIObject extends UnicastRemoteObject {



public YourRemoteObject( int port ) {

super( port, new ZipClientSocketFactory(), new ZipServerSocketFactory() );

}



// 剩下的是你自己的程序?qū)崿F(xiàn)



}



現(xiàn)在你的通信數(shù)據(jù)得到了壓縮.





關(guān)于作者:

Tony Sintes 是一個(gè)獨(dú)立咨詢?nèi)?同時(shí)也是First Class Consulting, Inc. 的創(chuàng)始人.這一咨詢公司主要致力與對(duì)各個(gè)不同的企業(yè)系統(tǒng)進(jìn)行量身定制和培訓(xùn) . 業(yè)余時(shí)間,Tony 是一個(gè)積極的自由作家,同時(shí)也是Sams出版的<<21天學(xué)通面向?qū)ο缶幊?gt;>的作者 (Sams, 2001; ISBN: 0672321092).



資源:

To download the source code that accompanies this article, go to:

http://www.javaworld.com/javaworld/javaqa/2001-12/ziprmi/01-qa-1207-ziprmi.zip



"Zip Your Data and Improve the Performance of Your Network-Based Applications," Todd Sundsted (JavaWorld, November 1998):

http://www.javaworld.com/javaworld/jw-11-1998/jw-11-howto.html



"Creating a Custom RMI Socket Factory," (Sun Microsystems, 1999):

http://java.sun.com/products/jdk/1.2/docs/guide/rmi/rmisocketfactory.doc.html



"Creating a Custom Socket Type," (Sun Microsystems, 1999):

http://java.sun.com/products/jdk/1.2/docs/guide/rmi/sockettype.doc.html



Be sure to check out Tony's suggested reading list at:

http://www.firstclassconsulting.net/reading.html



For more RMI stories, visit the RMI / RMI-IIOP section of JavaWorld's Topical Index:

http://www.javaworld.com/channel_content/jw-rmi-index.shtml



Want more? See the Java Q&A Index for the full Q&A catalog:

http://www.javaworld.com/columns/jw-qna-index.shtml



For over 100 insightful Java tips from some of the best minds in the business, visit JavaWorld's Java Tips Index:

http://www.javaworld.com/columns/jw-tips-index.shtml



Learn the basics of client-side Java in our Java Beginner discussion. Core topics include the Java language, the Java Virtual Machine, APIs, and development tools:

http://forums.idg.net/webx?50@@.ee6b804



Sign up for JavaWorld's free Applied Java newsletter:

http://www.idg.net/jw-subscribe



You'll find a wealth of IT-related articles from our sister publications at IDG.net

譯者:我覺(jué)得這篇文章很有實(shí)用性,并且告訴了我們一種在網(wǎng)絡(luò)通信時(shí)進(jìn)行數(shù)據(jù)壓縮的一個(gè)可行的解決辦法.所以把它翻譯出來(lái),希望大家都能學(xué)到這一方法.這篇文章翻譯起來(lái)比較簡(jiǎn)單.但是由于水平有限,如果不對(duì)的地方懇請(qǐng)大家指正. bootcool@263.net.

溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

本類教程下載

系統(tǒng)下載排行

主站蜘蛛池模板: 亚洲欧美一区二区三区不卡 | 中文字幕综合久久久久 | 亚洲无线乱码高清在线观看一区 | 欧美午夜小视频 | 91成人免费| 国产1区| 亚洲小视频网站 | 欧美成人在线免费 | 欧美黄色片在线播放 | 亚洲欧美在线视频免费 | 欧美日韩精品一区二区三区视频 | 天天综合影视 | 久久精品国产只有精品6 | 一级做人爱视频日本 | 又黄又爽又色还免费的视频 | 日本中文字幕在线精品 | 国产精品一区二区国产 | 亚洲国产成人欧美激情 | 一区二区日韩欧美 | 日韩毛片大全 | 色国产精品一区在线观看 | 久久精品综合国产二区 | 午夜网站在线观看免费网址免费 | 久久99国产精品一区二区 | 手机国产日韩高清免费看片 | 美女福利视频在线 | 日本在线看片网站 | 欧美一区二区三区免费播放 | 毛片99| 日本精品中文字幕在线不卡 | 欧美国产综合视频在线观看 | 亚洲乱亚洲乱妇13p 亚洲伦理久久 | 欧美日韩中出 | 欧美精品午夜久久久伊人 | 日韩欧美不卡在线 | 国产男女爱视频在线观看 | 久久精品综合一区二区三区 | 超碰免费公开 | 国产精品女上位好爽在线短片 | 天天插天天操天天干 | 狠狠躁天天躁夜夜躁夜天战 |