ImportJava.io.*;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classMultidownload {LongBeginTime; intThreadCount; intRunningthread; intfileSize; FileLog FileLog=NULL; PublicMultidownload (intThreadCount, FileLog filelog) { This. BeginTime =System.currenttimemillis (); This. ThreadCount =ThreadCount; This. Runningthread =ThreadCount; This. FileLog =FileLog; } Public voidmultithreaddownload (String sourcepath, File targetfile) {fileSize=GetFileSize (SourcePath); intBlockSize = fileSize/ThreadCount; System.out.println ("Blocksize=" +blockSize); for(intThreadId = 1; ThreadId <= ThreadCount; threadid++) { intBeginindex = (threadId-1) *blockSize; intEndIndex = threadId * blockSize-1; if(ThreadId = =threadcount) {EndIndex= FileSize-1; } System.out.println ("thread=" + threadId + "" + Beginindex + "--" +EndIndex); if(!Targetfile.getparentfile (). exists ()) Targetfile.getparentfile (). Mkdirs (); if(!targetfile.exists ())Try{targetfile.createnewfile (); } Catch(IOException e) {e.printstacktrace (); } NewDownloadthread (ThreadId, SourcePath, Beginindex, EndIndex, TargetFile). Start (); } } Public classDownloadthreadextendsThread {PrivateString SourcePath; Private intBeginindex; Private intEndIndex; Private intthreadId; PrivateFile targetfile; PublicDownloadthread (intThreadId, String SourcePath,intBeginindex,intEndIndex, File targetfile) { This. SourcePath =SourcePath; This. Beginindex =Beginindex; This. EndIndex =EndIndex; This. ThreadId =threadId; This. TargetFile =targetfile; } @Override Public voidrun () {HttpURLConnection conn=NULL; Randomaccessfile RAF; Try{RAF=NewRandomaccessfile (TargetFile, "RWD"); URL URL=NewURL (SourcePath); Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (10000); if(Filelog.getalreadydata (threadId)! =-1) Beginindex=Filelog.getalreadydata (threadId); Conn.setrequestproperty ("Range", "bytes=" + beginindex + "-" +EndIndex); intCode =Conn.getresponsecode (); intTotal = 0; if(Code = = 206) {InputStream is=Conn.getinputstream (); Raf.seek (Beginindex); intLen; byte[] Buff =New byte[1024]; while(len = is.read (buff))! =-1) {raf.write (buff,0, Len); Total+=Len; Filelog.set (String.valueof (threadId), total+ BeginIndex-1 + ""); //System.out.println ("thread" + threadId + "Start download =" + string.valueof (total + beginIndex-1));}} runningthread--; } Catch(IOException e) {e.printstacktrace (); } finally { if(Runningthread = = 0) { LongEndTime =System.currenttimemillis (); LongTime = (endtime-begintime)/1000; DoubleSpeed = (Double) filesize/1024/1024/Time ; System.out.println ("Download Completed" + Time + "seconds" + "" + math.ceil (Speed * +)/+ "MB/S"); Filelog.deletefile (); } if(Conn! =NULL) {conn.disconnect (); } } } } Private Static intGetFileSize (String sourcepath) {intFileSize = 0; HttpURLConnection Conn=NULL; Try{URL URL=NewURL (SourcePath); Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (10000); intCode =Conn.getresponsecode (); if(Code = = 200) {fileSize=conn.getcontentlength (); DoubleD = filesize/1024/1024; System.out.println ("Total File size =" + Math.ceil (d * +)/+ "MB"); } } Catch(java.io.IOException e) {e.printstacktrace (); } finally { if(Conn! =NULL) {conn.disconnect (); } } returnfileSize; }}
ImportJava.io.*;ImportJava.util.*; Public classFileLog {Properties Properties=NewProperties (); File file; Publicfilelog (file file) { This. File =file; if(!File.getparentfile (). exists ()) File.getparentfile (). Mkdirs (); if(!file.exists ())Try{file.createnewfile (); } Catch(IOException e) {e.printstacktrace (); } } Public synchronized voidSet (String key,string value) {InputStream is=NULL; OutputStream OS=NULL; Try{ is=Newfileinputstream (file); Properties.load (IS); Enumeration e=Properties.propertynames (); while(E.hasmoreelements ()) {String Mkey=(String) e.nextelement (); String Mvalue=Properties.getproperty (Mkey); if(Mkey.equals (key)) {Properties.setproperty (key, value); } Else{properties.setproperty (Mkey, Mvalue); }} properties.setproperty (key, value); Is.close (); OS=Newfileoutputstream (file); Properties.store (OS,""); Os.close (); } Catch(IOException e) {e.printstacktrace (); } } Publicstring Get (String key) {String value=NULL; Try{InputStream is=Newfileinputstream (file); Properties.load (IS); Value=Properties.getproperty (key); System.out.println ("FileLog get ()" + "key=" +key + "" + "value=" +value); } Catch(IOException e) {e.printstacktrace (); } returnvalue; } Public intGetalreadydata (intkey) { intdata =-1; if(File.exists () && file.length () >0) {String value=Get (string.valueof (key)); if(Value! =NULL) Data=Integer.parseint (value); } returndata; } Public voidGetAll () {InputStream is=NULL; Try{ is=Newfileinputstream (file); Properties.load (IS); Enumeration e=Properties.propertynames (); while(E.hasmoreelements ()) {String key=(String) e.nextelement (); String value=Properties.getproperty (key); //System.out.println (key+ "" +value); } } Catch(IOException e) {e.printstacktrace (); } } Public voidDeleteFile () {file.delete (); }}
String Targetpathdir = "z:\\"; = "Setup.exe"; = "Http://down.360safe.com/setup.exe"; New File (Targetpathdir, targetfilename); New FileLog (new File (Targetpathdir, targetfilename.substring (0,targetfilename.indexof ('. ')) + ". txt")) ; New Multidownload (2, filelog); Multidownload.multithreaddownload (SourcePath, targetfile);
Multi-threaded Breakpoint Download