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: