Multi-Threaded Download pictures

Source: Internet
Author: User
Tags stub

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

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.