Package org.load.download;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.RandomAccessFile;
Import Java.text.DecimalFormat;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.ClientProtocolException;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;
Import org.apache.http.impl.client.DefaultHttpClient;
public class Download {
public static void Main (string[] args) {
New Thread (New D ("Http://al.jb51.net:81/200812/tools/HA_LeapFTP.rar"))
. Start ();
New Thread (
New D (
"Http://al.jb51.net:81/200812/tools/HA_LeapFTP.rar"))
. Start ();
}
}
Class D implements Runnable {
private static final String PATH = "E:\\download";
Private String URL;
Private String fileName = null;
static {
if (!new File (PATH). Exists ()) {
New File (PATH). Mkdirs ();
}
}
Public D (String URL) {
This.url = URL;
This.filename = this.url.substring (this.url.lastIndexOf ('/') + 1,
This.url.length ()); Get file name
}
public void Download () throws Clientprotocolexception, IOException {
Final Randomaccessfile file = new Randomaccessfile (this. PATH + file.separator
+ This.filename, "RW");
HttpClient client = new Defaulthttpclient ();
HttpGet get = new HttpGet (This.url);
Client.getparams (). Setparameter ("Http.socket.timeout", 5000); Setting the connection timeout
Long localfilesize = This.getlocalfilesize ();
Final Long remotefilesize = This.getremotefilesize ();
If the local file is not downloaded, the breakpoint is downloaded
if ( -1!= localfilesize && 1!= remotefilesize
&& Localfilesize < remotefilesize) {
File.seek (localfilesize); Local flag
Get.addheader ("Range", "bytes=" + localfilesize + "-")
+ remotefilesize); Remote tag
}
If the local file size is greater than or equal to the remote file, the download is complete
if ( -1!= localfilesize && localfilesize >= remotefilesize) {
Return
}
Start download
HttpResponse response = Client.execute (get);
if (>= response.getstatusline (). Getstatuscode ()) {
Httpentity en = response.getentity ();
InputStream in = En.getcontent ();
Byte[] by = new BYTE[512];
int len =-1;
Show progress
New Thread (New Runnable () {
@Override
public void Run () {
try {
while (File.length () < remotefilesize) {
Runtime.getruntime (). EXEC ("cmd cls"); I hear it's going to be a different process.
System.out.println (fileName
+ "downloaded: \ t"
+ New DecimalFormat ("0.00%"). Format (file
. Length ()/(double) remotefilesize);
Thread.Sleep (5000);
}
catch (IOException e) {
E.printstacktrace ();
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
). Start ();
Start download
while ( -1!= (len = In.read (by)) {
File.write (by, 0, Len);
}
In.close ();
Client.getconnectionmanager (). Shutdown ();
}
}
Get local File size
Private Long getlocalfilesize () {
File File = new file (PATH + file.separator + this.filename);
if (!file.exists ()) {
return-1l;
}
return File.length ();
}
//Gets the remote file size
private long getremotefilesize () throws Clientprotocolexception,
ioexception {
httpclient client = new Defaulthttpclient ();
httpget get = new HttpGet (This.url);
client.getparams (). Setparameter ("Http.socket.timeout", 5000);
httpresponse response = Client.execute (get);
if = Response.getstatusline () getstatuscode ()
| | >= Response.getstatusline (). Getstatuscode ()) {
httpentity en = response.getentity ();
return en.getcontentlength ();
}
return-1l;
}
@Override
public void Run () {
try {
Download ();
System.out.println (this.filename + "\ t download complete");
catch (Clientprotocolexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
}