Java multi-thread download

Source: Internet
Author: User

 

Multiple files are downloaded at the same time, but a single file does not have multi-thread downloads and does not have the resumable upload function. Continue to improve:

 
Package com. util. file; public class files {/***** get the applicationProgram* @ Return application root directory */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 = new HTTP (); Private string webaddress = ""; private string destfile = ""; Public void setwebaddress (string webaddress) {This. weba Ddress = 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 );}}
 
 
 
Test passed:
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.