知识大全 Java多线程下载

Posted

篇首语:你不勇敢,没人替你坚强。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java多线程下载相关的知识,希望对你有一定的参考价值。

Java多线程下载  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

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

  view plaincopy to clipboardprint?    package util file;

  public class Files

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

  

  package 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 re crawl;

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

  import re Http;

  public class WebSpider implements Runnable

  private 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 Conn = null;

  try             URL url = new URL(webAddress);

  Conn = (HttpURLConnection) url openConnection();            ( GET );            ( User Agent Mozilla/ (Windows; U; Windows NT ; zh CN; rv: ) Gecko/ Firefox/ );            InputStream in = ();            String fileType = fileType(());            System out println(fileType);            FileOutputStream out = new FileOutputStream(new File(destFile + fileType));            int chByte = in read();            while (chByte != )             out write(chByte);            //System out println(chByte);            chByte = in read();                    catch (Exception ex)             System out println(ex toString());        finally             ();                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 re crawl;     import java io File;   import java io FileOutputStream;   import java io IOException;   import java io InputStream;   import HttpURLConnection;   import URL;     import re Http;     public class WebSpider implements Runnable              private 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 Conn = null;         try            URL url = new URL(webAddress);                    Conn = (HttpURLConnection) url openConnection();           ( GET );           ( User Agent Mozilla/ (Windows; U; Windows NT ; zh CN; rv: ) Gecko/ Firefox/ );           InputStream in = ();           String fileType = fileType(());           System out println(fileType);           FileOutputStream out = new FileOutputStream(new File(destFile + fileType));           int chByte = in read();           while (chByte != )            out write(chByte);           //System out println(chByte);           chByte = in read();                  catch (Exception ex)            System out println(ex toString());       finally            ();              return true;                public void run()        try            //System out println(Thread currentThread() getName());           download();           catch (IOException e)            e printStackTrace();       catch (InterruptedException e)            e printStackTrace();                   package re crawl;

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

  import re Http;

  public class WebSpider implements Runnable

  private 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 Conn = null;

  try          URL url = new URL(webAddress);

  Conn = (HttpURLConnection) url openConnection();         ( GET );         ( User Agent Mozilla/ (Windows; U; Windows NT ; zh CN; rv: ) Gecko/ Firefox/ );         InputStream in = ();         String fileType = fileType(());         System out println(fileType);         FileOutputStream out = new FileOutputStream(new File(destFile + fileType));         int chByte = in read();         while (chByte != )       out write(chByte);      //System out println(chByte);      chByte = in read();               catch (Exception ex)          System out println(ex toString());      finally          ();          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 re crawl;

  import java io IOException;

  import 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 spider = new WebSpider();        WebSpider spider = new WebSpider();        spider setWebAddress( );        spider setDestFile(Files getSysPath() + / + spider );

  spider setWebAddress( );        spider setDestFile(Files getSysPath() + / + spider );

  Thread t = new Thread(spider );        Thread t = new Thread(spider );        t start();        t start();

  t join();        t join();

  System out println( the end );        System out println(System currentTimeMillis() begin);       

cha138/Article/program/Java/gj/201311/27577

相关参考

知识大全 使用Java多线程实现任务分发

使用Java多线程实现任务分发  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  多线程下载由来已久

知识大全 Java的多线程-实现多线程及线程的同步

Java的多线程-实现多线程及线程的同步  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  一.实现

知识大全 Java多线程编程基础之线程和多线程

Java多线程编程基础之线程和多线程  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  随着计算机技

知识大全 Java多线程如何创建多个线程

Java多线程如何创建多个线程  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Java多线程的同

知识大全 超线程多核心下Java多线程编程分析

超线程多核心下Java多线程编程分析  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  一Java环

知识大全 超线程多核心下Java多线程编程技术分析[4]

超线程多核心下Java多线程编程技术分析[4]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nb

知识大全 超线程多核心下Java多线程编程技术分析[3]

超线程多核心下Java多线程编程技术分析[3]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nb

知识大全 超线程多核心下Java多线程编程技术分析[1]

超线程多核心下Java多线程编程技术分析[1]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nb

知识大全 超线程多核心下Java多线程编程技术分析[2]

超线程多核心下Java多线程编程技术分析[2]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nb

知识大全 Java多线程编程基础之线程对象

Java多线程编程基础之线程对象  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &nbs