工作小驛

          Ninja!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            103 Posts :: 0 Stories :: 36 Comments :: 0 Trackbacks
          序言

          許多用戶可能會遇到這樣的情況:在網(wǎng)站上發(fā)現(xiàn)一個很好的資源,但是這個資源是分成了很多個文件存放的,如果想把它保存到本地,只有靠用戶點(diǎn)擊另存來完成保存,如果資源分了幾百甚至上千上萬,那簡直是個災(zāi)難。

          在Internet上很多的資源分成多個文件存放時,它的文件命名是有一定的規(guī)則的;正因如此,我們就可以用程序來完成這個資源的完全下載。

          1. 基礎(chǔ)知識

          在Internet上,我們要下載網(wǎng)站上的某個資源,我們會獲得一個URL(Uniform Resource Locator),它是一個服務(wù)器資源定位的描述,下載的過程總是如下步驟:

          步驟1:客戶端發(fā)起連接請求一個URL

          步驟2:服務(wù)器解析URL,并將指定的資源返回一個輸入流給客戶

          步驟3:客戶端接收輸入流,將流中的內(nèi)容存到文件

          2. 網(wǎng)絡(luò)連接的建立

          Java提供了對URL訪問和大量的流操作的的API,我們可以很容易的完成對網(wǎng)絡(luò)上資源的存取,下面的代碼段就完成了對一個網(wǎng)站的資源進(jìn)行訪問:


          ......
          destUrl="http://www.ebook.com/java/網(wǎng)絡(luò)編程001.zip";
          url = new URL(destUrl);
          httpUrl = (HttpURLConnection) url.openConnection();
          //連接指定的網(wǎng)絡(luò)資源
          httpUrl.connect();
          //獲取網(wǎng)絡(luò)輸入流
          bis = new BufferedInputStream(httpUrl.getInputStream());
          ......


          3. 代理的訪問

          Java 中通過代理服務(wù)器訪問外網(wǎng)的方法已經(jīng)是世人皆知的秘密了。這里就不再多描述了,訪問的JAVA代碼如下:


          //設(shè)置代理服務(wù)器
          System.getProperties().put("proxySet", "true");
          System.getProperties().put("proxyHost", "10.154.134.110");
          System.getProperties().put("proxyPort", "8080");


          4. 網(wǎng)絡(luò)資源的保存

          在上節(jié)中,我們已經(jīng)獲取了指定網(wǎng)絡(luò)資源的輸入流,接下來我們要完成的就是讀取輸入流中的所以內(nèi)容,并將其保存在文件中。示例代碼:


          ......
          fos = new FileOutputStream(fileName);
          if (this.DEBUG)
          System.out.println
          ("正在獲取鏈接[" + destUrl + "]的內(nèi)容...
          將其保存為文件[" + fileName +"]");

          //保存文件
          while ( (size = bis.read(buf)) != -1)
          fos.write(buf, 0, size);
          ......


          上面的示例代碼就將網(wǎng)絡(luò)資源的內(nèi)容保存到了本地指定的文件中。

          5. 代碼清單


          import java.io.*;
          import java.net.*;
          import java.util.*;

          /**
          * <p>Title: 個人開發(fā)的API</p>
          * <p>Description: 將指定的HTTP網(wǎng)絡(luò)資源在本地以文件形式存放</p>
          * <p>Copyright: Copyright (c) 2004</p>
          * <p>Company: NewSky</p>
          * @author MagicLiao
          * @version 1.0
          */
          public class HttpGet {

            public final static boolean DEBUG = true;//調(diào)試用
            private static int BUFFER_SIZE = 8096;//緩沖區(qū)大小
            private Vector vDownLoad = new Vector();//URL列表
            private Vector vFileList = new Vector();//下載后的保存文件名列表

            /**
            * 構(gòu)造方法
            */
            public HttpGet() {}

            /**
            * 清除下載列表
            */
            public void resetList() {
             vDownLoad.clear();
             vFileList.clear();
            }

            /**
            * 增加下載列表項
            *
            * @param url String
            * @param filename String
            */

          public void addItem(String url, String filename) {
            vDownLoad.add(url);
            vFileList.add(filename);
          }

            /**
            * 根據(jù)列表下載資源
            */
          public void downLoadByList() {
            String url = null;
            String filename = null;

            //按列表順序保存資源
            for (int i = 0; i < vDownLoad.size(); i++) {
             url = (String) vDownLoad.get(i);
             filename = (String) vFileList.get(i);

             try {
              saveToFile(url, filename);
             }
             catch (IOException err) {
              if (DEBUG) {
               System.out.println("資源[" + url + "]下載失敗!!!");
              }
             }
            }

            if (DEBUG) {
             System.out.println("下載完成!!!");
            }
          }

          /**
          * 將HTTP資源另存為文件
          *
          * @param destUrl String
          * @param fileName String
          * @throws Exception
          */
          public void saveToFile(String destUrl, String fileName)throws IOException{
            FileOutputStream fos = null;
            BufferedInputStream bis = null;
            HttpURLConnection httpUrl = null;
            URL url = null;
            byte[] buf = new byte[BUFFER_SIZE];
            int size = 0;

            //建立鏈接
            url = new URL(destUrl);
            httpUrl = (HttpURLConnection) url.openConnection();
            //連接指定的資源
            httpUrl.connect();
            //獲取網(wǎng)絡(luò)輸入流
            bis = new BufferedInputStream(httpUrl.getInputStream());
            //建立文件
            fos = new FileOutputStream(fileName);

            if (this.DEBUG)
           System.out.println
          ("正在獲取鏈接[" + destUrl + "]的內(nèi)容...
          將其保存為文件[" + fileName + "]");

            //保存文件
            while ( (size = bis.read(buf)) != -1)
             fos.write(buf, 0, size);

            fos.close();
            bis.close();
            httpUrl.disconnect();
          }

          /**
          * 設(shè)置代理服務(wù)器
          *
          * @param proxy String
          * @param proxyPort String
          */
          public void setProxyServer(String proxy, String proxyPort) {
            //設(shè)置代理服務(wù)器
            System.getProperties().put("proxySet", "true");
            System.getProperties().put("proxyHost", proxy);
            System.getProperties().put("proxyPort", proxyPort);
          }

          /**
          * 設(shè)置認(rèn)證用戶名與密碼
          *
          * @param uid String
          * @param pwd String
          */
          public void setAuthenticator(String uid, String pwd) {
          Authenticator.setDefault(new MyAuthenticator(uid, pwd));
          }

          /**
          * 主方法(用于測試)
          *
          * @param argv String[]
          */
          public static void main(String argv[]) {
            HttpGet oInstance = new HttpGet();
            try {
            //增加下載列表(此處用戶可以寫入自己代碼來增加下載列表)
            oInstance.addItem("http://www.ebook.com/java/網(wǎng)絡(luò)編程001.zip","./網(wǎng)絡(luò)編程1.zip");
            oInstance.addItem("http://www.ebook.com/java/網(wǎng)絡(luò)編程002.zip","./網(wǎng)絡(luò)編程2.zip");
            oInstance.addItem("http://www.ebook.com/java/網(wǎng)絡(luò)編程003.zip","./網(wǎng)絡(luò)編程3.zip");
            oInstance.addItem("http://www.ebook.com/java/網(wǎng)絡(luò)編程004.zip","./網(wǎng)絡(luò)編程4.zip");
            oInstance.addItem("http://www.ebook.com/java/網(wǎng)絡(luò)編程005.zip","./網(wǎng)絡(luò)編程5.zip");
            oInstance.addItem("http://www.ebook.com/java/網(wǎng)絡(luò)編程006.zip","./網(wǎng)絡(luò)編程6.zip");
            oInstance.addItem("http://www.ebook.com/java/網(wǎng)絡(luò)編程007.zip","./網(wǎng)絡(luò)編程7.zip");
            //開始下載
            oInstance.downLoadByList();
            }
            catch (Exception err) {
             System.out.println(err.getMessage());
            }
          }
          }
          posted on 2007-07-31 17:55 王君 閱讀(251) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 金湖县| 罗甸县| 新宁县| 磴口县| 准格尔旗| 罗定市| 农安县| 南汇区| 临海市| 宁陵县| 潞城市| 门头沟区| 怀仁县| 甘谷县| 双桥区| 永州市| 湘潭县| 高邮市| 桃园市| 南川市| 濮阳市| 苍南县| 邯郸市| 新源县| 夹江县| 仙居县| 灌阳县| 历史| 新邵县| 河间市| 肥西县| 龙川县| 台北市| 敖汉旗| 尉氏县| 阳春市| 丽江市| 汉阴县| 文山县| 洛扎县| 三台县|