<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 鴻雁 閱讀(664) 評論(0)  編輯  收藏

    主站蜘蛛池模板: 日韩精品无码免费专区午夜不卡| 国产在亚洲线视频观看| 免费精品一区二区三区第35 | 亚洲欧洲日产国码无码久久99| 国产偷国产偷亚洲高清人| 四虎AV永久在线精品免费观看| 午夜亚洲国产精品福利| 亚洲国产成人久久综合一区77 | 亚洲精品无码久久久影院相关影片| 一级毛片高清免费播放| 国产亚洲老熟女视频| 精品国产呦系列在线观看免费| 亚洲中文字幕日产乱码高清app | 亚洲国产高清在线精品一区| 国产a视频精品免费观看| 亚洲最大福利视频| 日韩一级视频免费观看| 免费人成视频在线观看免费| 国产亚洲AV夜间福利香蕉149| 国产成人免费ā片在线观看老同学| 久久国产亚洲高清观看| 91免费播放人人爽人人快乐| 亚洲日韩国产一区二区三区在线 | 狠狠综合亚洲综合亚洲色| 亚洲?v无码国产在丝袜线观看| 99re8这里有精品热视频免费| 亚洲精品第一国产综合精品| 精品无码国产污污污免费| 国产免费久久精品丫丫| 亚洲日本香蕉视频| mm1313亚洲国产精品美女| 国产亚洲免费的视频看| 亚洲永久在线观看| 久久夜色精品国产亚洲av | 国产色爽女小说免费看| 两性色午夜视频免费播放| 久久综合亚洲色HEZYO社区 | 久久免费动漫品精老司机| 亚洲一区二区三区不卡在线播放| 亚洲国产综合自在线另类| 久久亚洲欧美国产精品|