注册本站  论坛  繁體中文

电脑技巧
手机 | MP3 | MP4 | 显卡 | 主板 | 显示器 | 光存储 | 笔记本 | 网络设备 | 移动存储 | 数码相机
键鼠 | CPU | 音箱 | GPS | 电视 | 服务器 | 投影机 | 机箱电源 | 品牌电脑 | 办公打印 |
| 网站首页 | Cisco | Windows | Linux | Java | Dotnet | Oracle | 网页设计 | 平面设计 | 安全 | 软件应用 | 电脑维修 | 办公维修 |
您现在的位置: 电脑技巧 >> Java >> 高级技术 >> 多线程 >> Java正文

Java多线程下载

文章来源:中国哦IT实验室收集整理 作者:佚名 更新时间:2008-6-28 20:33:18 【 】 【加入收藏

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

    view plaincopy to clipboardprint?
    package com.util.file;

    public class Files {

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


    }

    package com.util.file;

    public class Files {

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


    }view plaincopy to clipboardprint?


     view plaincopy to clipboardprint?
    <PRE class=csharp name="code">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();
        }
        }
    }
    </PRE>

    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;

    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();
     }
        }
    }

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

    import java.io.IOException;

    import com.util.file.Files;

    public class Crawl {

        /**
         * @param args
         * @throws IOException
         * @throws InterruptedException
         */
        public static void main(String[] args) throws IOException, InterruptedException {

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

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

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

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

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

 

    }</PRE>
    <PRE class=csharp name="code"> </PRE>
    <PRE class=csharp name="code">测试通过:</PRE>
    <PRE class=csharp name="code"></PRE>

  • 上一篇Java:

  • 下一篇Java: 没有了
  • 最 新 热 门
     手机开发平台指南、教程和资料介绍
     关于什么叫面向接口编程
     编写高级JavaScript应用代码
     不要验证,直接转化科学计数法
     Eclipse插件开发中实现刷新和重编译介绍
     Java开源技术:Eclipse的使用技巧详解
     配置eclipse 3.2 使用JDK1.5中文JavaAPI
     集成Windows本地应用到Eclipse RCP 程序中
     hibernate.cfg.xml配置文件的说明
     eclipse开发jface时,main.class解决方法
    最 新 推 荐
     Java多线程下载
     关于线程池的简单构建
     使用技巧:Java程序开发中如何应用线程
     Java多线程Socket操作猜数游戏样例
     100行Java代码构建一个线程池。
     Java多线程同步设计中使用Metux
     多线程编程的设计模式 临界区模式
     Java语言深入 多线程程序模型研究
     多线程在JAVA ME应用程序中的使用
     扫描整个网段的多线程程序
    相 关 文 章

    编写高级JavaScript应用代码
    Java开源技术:Eclipse的使用技巧详解
    配置eclipse 3.2 使用JDK1.5中文JavaAPI
    Java WebService 整理笔记
    深入探索 高效的Java异常处理框架
    怎样减小JAR文件大小
    妙用异步Servlet扩展AJAX应用程序
    java.servlet.Filter的应用
    JAVAMAIL邮件服务器
    一个简单JAVA网络通讯录

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告

     

    Copyright 2006-2008 pcjx.com All Rights Reserved
    电脑技巧 版权所有 粤ICP备06059145号 地图
    本网站所有内容未经许可不得转载或做其他使用