Package cn.me.test;
Import Java.io.InputStream;
Import Java.io.RandomAccessFile;
Import java.net.HttpURLConnection;
Import Java.net.URL;
/**
* Multi-threaded download
* 1: Write data in any location using Randomaccessfile.
* 2: The amount of data downloaded by the first thread needs to be calculated and distributed evenly. If the average is not enough,
* Direct the last thread to handle the relatively small amount of data
* 3: Must be prepared before downloading the same size of files, through the file header to get
*/
public class Multithreaddownload {
public static void Main (string[] args) throws Exception {
1: Declaring the filename and the address of the download
String fileName = "Aa.rar";
String urlstr = "Http://localhost:7777/day18";
2: Declaration URL
URL url = new URL (urlstr+ "/" +filename);
3: Get the connection
HttpURLConnection con =
(httpurlconnection) url.openconnection ();
4: Set the request mode
Con.setrequestmethod ("get");
5: Get the request header, that is, the length of the file
int length = Con.getcontentlength ();//Gets the length of the download file to calculate the amount of data each thread should download.
6: Create a file of the same size in the specified directory
Randomaccessfile file = new Randomaccessfile ("d:/a/" +filename, "RW");//Create a document of the same size.
7: Set the file size, placeholder
File.setlength (length);//Set File size.
File.close ();
8: Define the number of threads
int size = 3;
9: Calculate how many bytes each thread should download the data, if the exact division is the best, otherwise add 1
int block = length/size==0?length/size:length/size+1;//Calculates the amount of data that each thread should download.
SYSTEM.ERR.PRINTLN ("Each thread should be downloaded:" +block);
10: Run three threads and calculate from which byte to start to which byte end
for (int i=0;i<size;i++) {
int start = I*block;
int end = start+ (block-1);//calculate start and end bytes for each thread.
System.err.println (i+ "=" +start+ "," +end);
New Mydownthread (FileName, Start, End,url). Start ();
}
}
Static class Mydownthread extends thread{
Define file name
Private String FileName;
Define where to start the download
private int start;
Define which byte to download
private int end;
Private URL URL;
Public Mydownthread (String filename,int start,int end,url URL) {
This.filename=filename;
This.start=start;
This.end=end;
This.url=url;
}
@Override
public void Run () {
try{
11: Start downloading
HttpURLConnection con =
(httpurlconnection) url.openconnection ();
Con.setrequestmethod ("get");
12: Set the request header for the segmented download
Con.setrequestproperty ("Range", "bytes=" +start+ "-" +end),//sets the block of files read from the server.
13: Start downloading, need to Judge 206
if (Con.getresponsecode () ==206) {//Access succeeded, the returned status code is 206.
InputStream in = Con.getinputstream ();
14: Declare random Write file objects, note that RWD is to write data to the file immediately, without using a buffer
Randomaccessfile out = new Randomaccessfile ("d:/a/" +filename, "RWD");
Out.seek (start);/setting starts writing data from a location in the file.
Byte[] B=new byte[1024];
int len = 0;
while ((Len=in.read (b))!=-1) {
Out.write (B,0,len);
}
Out.close ();
In.close ();
}
System.err.println (This.getname () + "execution complete");
}catch (Exception e) {
throw new RuntimeException (e);
}
}
}
}