熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

Java多線程下載

2022-06-13   來源: Java高級技術 

  同時下載多個文件不過單文件沒有多線程的下載並且沒有斷點續傳功能繼續完善:

  view plaincopy to clipboardprint?
    package comutilfile;

  public class Files {

  /***
         * 獲取應用程序的根目錄
         * @return 應用程序根目錄
         */
        public static String getSysPath(){
        return  SystemgetProperty(userdir);
        }

  }

  package comutilfile;

  public class Files {

  /***
         * 獲取應用程序的根目錄
         * @return 應用程序根目錄
         */
        public static String getSysPath(){
     return  SystemgetProperty(userdir);
        }

  }view plaincopy to clipboardprint?

  view plaincopy to clipboardprint?
    <PRE class=csharp name=code>package recrawl;

  import javaioFile;
    import javaioFileOutputStream;
    import javaioIOException;
    import javaioInputStream;
    import HttpURLConnection;
    import URL;

  import rehttpHttp;

  public class WebSpider implements Runnable{

  private Http http = new Http();

  private String webAddress = ;
        private String destFile = ;

  public void setWebAddress(String webAddress){
        thiswebAddress = webAddress;
        }

  public void setDestFile (String destFile){
        thisdestFile = destFile;
        }

  public boolean download() throws IOException InterruptedException {

  HttpURLConnection httpConn = null;

  try {
            URL url = new URL(webAddress);

  httpConn = (HttpURLConnection) urlopenConnection();
            (GET);
            (UserAgent Mozilla/ (Windows; U; Windows NT ; zhCN; rv:) Gecko/ Firefox/);
            InputStream in = ();
            String fileType = httpfileType(());
            Systemoutprintln(fileType);
            FileOutputStream out = new FileOutputStream(new File(destFile + fileType));
            int chByte = inread();
            while (chByte != ) {
            outwrite(chByte);
            //Systemoutprintln(chByte);
            chByte = inread();
            }
        } catch (Exception ex) {
            Systemoutprintln(extoString());
        } finally {
            ();
        }
        return true;
        }

  public void run() {
        try {
            //Systemoutprintln(ThreadcurrentThread()getName());
            download();
        } catch (IOException e) {
            eprintStackTrace();
        } catch (InterruptedException e) {
            eprintStackTrace();
        }
        }
    }
    </PRE>

  view plaincopy to clipboardprint?package recrawl;     import javaioFile;   import javaioFileOutputStream;   import javaioIOException;   import javaioInputStream;   import HttpURLConnection;   import URL;     import rehttpHttp;     public class WebSpider implements Runnable{              private Http http = new Http();         private String webAddress = ;       private String destFile = ;              public void setWebAddress(String webAddress){       thiswebAddress = webAddress;       }              public void setDestFile (String destFile){       thisdestFile = destFile;       }              public boolean download() throws IOException InterruptedException {         HttpURLConnection httpConn = null;         try {           URL url = new URL(webAddress);                    httpConn = (HttpURLConnection) urlopenConnection();           (GET);           (UserAgent Mozilla/ (Windows; U; Windows NT ; zhCN; rv:) Gecko/ Firefox/);           InputStream in = ();           String fileType = httpfileType(());           Systemoutprintln(fileType);           FileOutputStream out = new FileOutputStream(new File(destFile + fileType));           int chByte = inread();           while (chByte != ) {           outwrite(chByte);           //Systemoutprintln(chByte);           chByte = inread();           }       } catch (Exception ex) {           Systemoutprintln(extoString());       } finally {           ();       }       return true;       }         public void run() {       try {           //Systemoutprintln(ThreadcurrentThread()getName());           download();           } catch (IOException e) {           eprintStackTrace();       } catch (InterruptedException e) {           eprintStackTrace();       }       }   }  package recrawl;

  import javaioFile;
    import javaioFileOutputStream;
    import javaioIOException;
    import javaioInputStream;
    import HttpURLConnection;
    import URL;

  import rehttpHttp;

  public class WebSpider implements Runnable{

  private Http http = new Http();

  private String webAddress = ;
        private String destFile = ;

  public void setWebAddress(String webAddress){
     thiswebAddress = webAddress;
        }

  public void setDestFile (String destFile){
     thisdestFile = destFile;
        }

  public boolean download() throws IOException InterruptedException {

  HttpURLConnection httpConn = null;

  try {
         URL url = new URL(webAddress);

  httpConn = (HttpURLConnection) urlopenConnection();
         (GET);
         (UserAgent Mozilla/ (Windows; U; Windows NT ; zhCN; rv:) Gecko/ Firefox/);
         InputStream in = ();
         String fileType = httpfileType(());
         Systemoutprintln(fileType);
         FileOutputStream out = new FileOutputStream(new File(destFile + fileType));
         int chByte = inread();
         while (chByte != ) {
      outwrite(chByte);
      //Systemoutprintln(chByte);
      chByte = inread();
         }
     } catch (Exception ex) {
         Systemoutprintln(extoString());
     } finally {
         ();
     }
     return true;
        }

  public void run() {
     try {
         //Systemoutprintln(ThreadcurrentThread()getName());
         download();
     } catch (IOException e) {
         eprintStackTrace();
     } catch (InterruptedException e) {
         eprintStackTrace();
     }
        }
    }

  view plaincopy to clipboardprint?
    <PRE class=csharp name=code>package recrawl;

  import javaioIOException;

  import comutilfileFiles;

  public class Crawl {

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

  long begin = SystemcurrentTimeMillis();
        WebSpider spider = new WebSpider();
        WebSpider spider = new WebSpider();
        spidersetWebAddress();
        spidersetDestFile(FilesgetSysPath() + /+spider);

  spidersetWebAddress();
        spidersetDestFile(FilesgetSysPath() + /+spider);

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

  tjoin();
        tjoin();

  Systemoutprintln(the end);
        Systemoutprintln(SystemcurrentTimeMillis() begin);
        }

  }</PRE>
    <PRE class=csharp name=code> </PRE>
    <PRE class=csharp name=code>測試通過</PRE>
    <PRE class=csharp name=code></PRE>


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27577.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.