Java多線程下載

來源:互聯網
上載者:User

 

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

package com.util.file;public class Files {    /***     * 擷取應用程式的根目錄     * @return 應用程式根目錄     */    public static String getSysPath(){return  System.getProperty("user.dir");     }    }
 
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.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);    }    }
 
測試通過:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.