Package mutidownload;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.RandomAccessFile;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
/**
* Multi-threaded download file
* @author Administrator
*
*/
public class Demo {
Public final static int threadcount=3;
public static void Main (string[] args) throws IOException {
TODO auto-generated Method Stub
Gets the length of the file on the server, creating a temporary file that is as large as the file on the server locally
String pathstring= "http://192.168.1.84:8091/androidServer/test.mkv";
URL url=new url (pathstring);
HttpURLConnection connection= (httpurlconnection) url.openconnection ();
Connection.setconnecttimeout (5000);
Connection.setrequestmethod ("GET");
int Code=connection.getresponsecode ();
if (code==200) {//Request succeeded
int Length=connection.getcontentlength ();
System.out.println ("File length:" +length);
Randomaccessfile ref=new randomaccessfile ("test.mkv", "RWD");
Specifies the length of the temporary file created
Ref.setlength (length);
Ref.close ();
int blocksize=length/threadcount;
for (int threadid=1;threadid<=threadcount;threadid++) {
int startindex= (threadId-1) *blocksize; Thread Start position
int endindex=threadid*blocksize-1;
if (Threadid==threadcount) {
Endindex=length;
}
SYSTEM.OUT.PRINTLN ("Thread" +threadid+ "Download---" +startindex+ "--" +endindex);
New Downhread (ThreadId, StartIndex, EndIndex, pathstring). Start ();
}
}else{
SYSTEM.OUT.PRINTLN ("Server Error");
}
}
/**
* Download file sub-thread
* @author Administrator
*
*/
public static class Downhread extends Thread {
private int threadId;
private int startIndex;
private int endIndex;
Private String path;
/**
*
* @param threadId Thread ID
* @param the starting position of StartIndex thread download
* @param endIndex
* @param the location of the path download file on the server
*/
Public downhread (int threadId, int startIndex, int endIndex, String path) {
Super ();
This.threadid = threadId;
This.startindex = StartIndex;
This.endindex = EndIndex;
This.path = path;
}
@Override
public void Run () {
TODO auto-generated Method Stub
Super.run ();
try {
URL url=new url (path);
HttpURLConnection connection= (httpurlconnection) url.openconnection ();
Connection.setconnecttimeout (5000);
Connection.setrequestmethod ("GET");
Connection.setrequestproperty ("Range", "bytes=" +startindex+ "-" +endindex);
int Code=connection.getresponsecode ();
System.out.println ("code:" +code);
if (code==200) {//Request succeeded
//
// }
InputStream Is=connection.getinputstream ();
Randomaccessfile raf=new randomaccessfile ("test.mkv", "RWD");
When writing a file, specify from which location to start writing
Raf.seek (StartIndex);
int len=0;
byte [] buffer=new byte[1024];
while ((Len=is.read (buffer))!=-1) {
Raf.write (buffer, 0, Len);
}
Is.close ();
Raf.close ();
System.out.println ("Thread:" +threadid+ "Download Complete ...");
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
Multi-Threaded Download pictures