The important thing in life is to have a great aim , and the determination

          常用鏈接

          統計

          IT技術鏈接

          保險相關

          友情鏈接

          基金知識

          生活相關

          最新評論

          用Java實現HTTP文件隊列下載

          ?代碼清單

          import java.io.*;
          import java.net.*;
          import java.util.*;
          /**
          * <p>title: 個人開發的api</p>
          * <p>description: 將指定的http網絡資源在本地以文件形式存放</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;//調試用
          private static int buffer_size = 8096;//緩沖區大小
          private vector vdownload = new vector();//url列表
          private vector vfilelist = new vector();//下載后的保存文件名列表
          /**
          * 構造方法
          */
          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);
          }
          /**
          * 根據列表下載資源
          */
          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();
          //獲取網絡輸入流
          bis = new bufferedinputstream(httpurl.getinputstream());
          //建立文件
          fos = new fileoutputstream(filename);
          if (this.debug)
          system.out.println("正在獲取鏈接[" + desturl + "]的內容...\n將其保存為文件[" + filename + "]");
          //保存文件
          while ( (size = bis.read(buf)) != -1)
          fos.write(buf, 0, size);

          fos.close();
          bis.close();
          httpurl.disconnect();
          }
          /**
          * 設置代理服務器
          *
          * @param proxy string
          * @param proxyport string
          */
          public void setproxyserver(string proxy, string proxyport) {
          //設置代理服務器
          system.getproperties().put("proxyset", "true");
          system.getproperties().put("proxyhost", proxy);
          system.getproperties().put("proxyport", proxyport);
          }
          /**
          * 設置認證用戶名與密碼
          *
          * @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/網絡編程001.zip","./網絡編程1.zip");
          oinstance.additem("http://www.ebook.com/java/網絡編程002.zip","./網絡編程2.zip");
          oinstance.additem("http://www.ebook.com/java/網絡編程003.zip","./網絡編程3.zip");
          oinstance.additem("http://www.ebook.com/java/網絡編程004.zip","./網絡編程4.zip");
          oinstance.additem("http://www.ebook.com/java/網絡編程005.zip","./網絡編程5.zip");
          oinstance.additem("http://www.ebook.com/java/網絡編程006.zip","./網絡編程6.zip");
          oinstance.additem("http://www.ebook.com/java/網絡編程007.zip","./網絡編程7.zip");
          //開始下載
          oinstance.downloadbylist();
          }
          catch (exception err) {
          system.out.println(err.getmessage());
          }
          }
          }

          posted on 2007-03-13 17:35 鴻雁 閱讀(666) 評論(0)  編輯  收藏

          主站蜘蛛池模板: 长汀县| 墨脱县| 白河县| 宣汉县| 黄大仙区| 同仁县| 吉木乃县| 东乡县| 崇左市| 台湾省| 绥化市| 鹤峰县| 临漳县| 瓦房店市| 江山市| 河源市| 舞钢市| 潮安县| 雷波县| 玛曲县| 张家界市| 句容市| 大连市| 太白县| 浏阳市| 顺平县| 德格县| 大洼县| 资中县| 永吉县| 从江县| 黄骅市| 高唐县| 湟中县| 双鸭山市| 随州市| 武功县| 察雅县| 林芝县| 龙胜| 临猗县|