网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > JAVA > 高级技术 > 多线程 > Java多线程下载  

Java多线程下载

2008-06-10  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

    同时下载多个文件,不过单文件没有多线程的下载并且没有断点续传功能,继续完善:

网管联盟bitsCN@com

    view plaincopy to clipboardprint?
    package com.util.file; 网管u家u.bitsCN.com

    public class Files { 网管论坛bbs_bitsCN_com

        /***
         * 获取应用程序的根目录
         * @return 应用程序根目录
         */
        public static String getSysPath(){
        return  System.getProperty("user.dir");
        } 网管网www_bitscn_com


    }

网管网www.bitscn.com

    package com.util.file; 网管下载dl.bitscn.com

    public class Files { 网管网www_bitscn_com

        /***
         * 获取应用程序的根目录
         * @return 应用程序根目录
         */
        public static String getSysPath(){
     return  System.getProperty("user.dir");
        }

网管下载dl.bitscn.com


    }view plaincopy to clipboardprint? 网管下载dl.bitscn.com


     view plaincopy to clipboardprint?
    <PRE class=csharp name="code">package com.core.crawl;

网管论坛bbs_bitsCN_com

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL; 网管下载dl.bitscn.com

    import com.core.http.Http;

网管网www_bitscn_com

    public class WebSpider implements Runnable{ 网管bitscn_com

        private Http http = new Http();

网管论坛bbs_bitsCN_com

        private String webAddress = "";
        private String destFile = "";

网管下载dl.bitscn.com

        public void setWebAddress(String webAddress){
        this.webAddress = webAddress;
        } 网管bitscn_com

        public void setDestFile (String destFile){
        this.destFile = destFile;
        }

网管联盟bitsCN_com

        public boolean download() throws IOException, InterruptedException {

网管网www.bitscn.com

        HttpURLConnection httpConn = null; 网管联盟bitsCN@com

        try {
            URL url = new URL(webAddress); 中国网管联盟bitsCN.com

            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestMethod("GET");
            httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14");
            InputStream in = httpConn.getInputStream();
            String fileType = http.fileType(httpConn.getContentType());
            System.out.println(fileType);
            FileOutputStream out = new FileOutputStream(new File(destFile + fileType));
            int chByte = in.read();

网管联盟bitsCN_com


            while (chByte != -1) {
            out.write(chByte);
            //System.out.println(chByte);
            chByte = in.read();
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        } finally {
            httpConn.disconnect();
        }
        return true;
        } 网管u家u.bitsCN.com

        public void run() {
        try {
            //System.out.println(Thread.currentThread().getName());
            download();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
    }
    </PRE> 网管u家u.bitsCN.com

    view plaincopy to clipboardprint?package com.core.crawl;     import java.io.File;   import java.io.FileOutputStream;   import java.io.IOException;   import java.io.InputStream;   import java.net.HttpURLConnection;   import java.net.URL;     import com.core.http.Http;     public class WebSpider implements Runnable{              private Http http = new Http();         private String webAddress = "";       private String destFile = "";              public void setWebAddress(String webAddress){       this.webAddress = webAddress;       }              public void setDestFile (String destFile){       this.destFile = destFile;       }              public boolean download() throws IOException, InterruptedException {         HttpURLConnection httpConn = null;         try {           URL url = new URL(webAddress);                    httpConn = (HttpURLConnection) url.openConnection();           httpConn.setRequestMethod("GET");           httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14");           InputStream in = httpConn.getInputStream();           String fileType = http.fileType(httpConn.getContentType());           System.out.println(fileType);           FileOutputStream out = new FileOutputStream(new File(destFile + fileType));           int chByte = in.read();           while (chByte != -1) {           out.write(chByte);           //System.out.println(chByte);           chByte = in.read();           }       } catch (Exception ex) {           System.out.println(ex.toString());       } finally {           httpConn.disconnect();       }       return true;       }         public void run() {       try {           //System.out.println(Thread.currentThread().getName());           download();           } catch (IOException e) {           e.printStackTrace();       } catch (InterruptedException e) {           e.printStackTrace();       }       }   }  package com.core.crawl;

网管u家u.bitsCN.com

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;

网管网www_bitscn_com

    import com.core.http.Http;

网管论坛bbs_bitsCN_com

    public class WebSpider implements Runnable{ 网管网www_bitscn_com

        private Http http = new Http();

网管联盟bitsCN@com

        private String webAddress = "";
        private String destFile = "";

网管bitscn_com

        public void setWebAddress(String webAddress){
     this.webAddress = webAddress;
        }

中国网管联盟bitsCN.com

        public void setDestFile (String destFile){
     this.destFile = destFile;
        }

网管u家u.bitsCN.com

        public boolean download() throws IOException, InterruptedException {

网管论坛bbs_bitsCN_com

     HttpURLConnection httpConn = null;

网管u家u.bitscn@com

     try {
         URL url = new URL(webAddress); 网管论坛bbs_bitsCN_com

         httpConn = (HttpURLConnection) url.openConnection();
         httpConn.setRequestMethod("GET");
         httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14");
         InputStream in = httpConn.getInputStream();
         String fileType = http.fileType(httpConn.getContentType());
         System.out.println(fileType);
         FileOutputStream out = new FileOutputStream(new File(destFile + fileType));
         int chByte = in.read();
         while (chByte != -1) {
      out.write(chByte);
      //System.out.println(chByte);

网管bitscn_com


      chByte = in.read();
         }
     } catch (Exception ex) {
         System.out.println(ex.toString());
     } finally {
         httpConn.disconnect();
     }
     return true;
        } 网管联盟bitsCN_com

        public void run() {
     try {
         //System.out.println(Thread.currentThread().getName());
         download();
     } catch (IOException e) {
         e.printStackTrace();
     } catch (InterruptedException e) {
         e.printStackTrace();
     }
        }
    }

网管网www.bitscn.com

    view plaincopy to clipboardprint?
    <PRE class=csharp name="code">package com.core.crawl; 中国网管联盟bitsCN.com

    import java.io.IOException;

网管网www.bitscn.com

    import com.util.file.Files;

网管下载dl.bitscn.com

    public class Crawl { 中国网管论坛bbs.bitsCN.com

        /**
         * @param args
         * @throws IOException
         * @throws InterruptedException
         */
        public static void main(String[] args) throws IOException, InterruptedException { 中国网管论坛bbs.bitsCN.com

        long begin = System.currentTimeMillis();
        WebSpider spider2 = new WebSpider();
        WebSpider spider1 = new WebSpider();
        spider1.setWebAddress("http://www.163.com");
        spider1.setDestFile(Files.getSysPath() + "/"+"spider1.");

中国网管联盟bitsCN.com

        spider2.setWebAddress("http://blog.csdn.net/longronglin");
        spider2.setDestFile(Files.getSysPath() + "/"+"spider2."); 网管bitscn_com

        Thread t1 = new Thread(spider1);
        Thread t2 = new Thread(spider2);
        t1.start();
        t2.start();

网管u家u.bitscn@com

        t1.join();
        t2.join();

网管网www.bitscn.com

        System.out.println("the end");
        System.out.println(System.currentTimeMillis() - begin);
        }

网管下载dl.bitscn.com

  网管网www.bitscn.com

    }</PRE>
    <PRE class=csharp name="code"> </PRE>
    <PRE class=csharp name="code">测试通过:</PRE>
    <PRE class=csharp name="code"></PRE>
网管u家u.bitsCN.com


TAGs
 上一篇:关于线程池的简单构建   下一篇:没有了
Java多线程下载 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: