Java Network Programming Note 2
To implement multi-threaded download, you can define the following steps:
1) create a URL object (URL indicates a Uniform Resource Locator)
URL url = new URL (path); // path resource path
2) obtain the size of the URL object pointing to the resource, and use the URLConnection class. This class represents the communication link between the application and the URL.
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setRequestMethod (GET); // GET by GET
Conn. setRequestProperty (Accept-Language, zh-CN );
Conn. setRequestProperty (Charset, UTF-8 );
Conn. setRequestProperty (Connection, Keep-Alive );
FileSize = conn. getContentLength (); // get the file size
Conn. disconnect ();
3) create an empty file of the same size as the network resource on the local disk.
RandomAccessFile files = new RandomAccessFile (targetFile, rw );
Files. setLength (fileSize); // you can specify the file size.
Files. close (); // close the file
4) create a thread, calculate the resource size of each thread, and start the thread.
The Code is as follows:
Import java. io. randomAccessFile; import java.net. httpURLConnection; import java.net. URL; public class DownUtil {private String path; // private String targetFile; // file storage location private int threadNumber; // Number of downloaded threads private int fileSize; // total file size private DownThread [] downThreads; // download the thread object public DownUtil (String path, String targetFile, int threadnumber1_1_this.path1_path1_this.tar getFile = targetFile; this. threadNumber = threadNumber; downThreads = new DownThread [threadNumber]; // initialization array} public void download () throws Exception {URL url = new URL (path); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setRequestMethod (GET); conn. setRequestProperty (Accept-Language, zh-CN); conn. setRequestProperty (Charset, UTF-8); conn. setRequestProperty (Connection, Keep-Alive); fileSize = conn. getContentLength (); // get the file size conn. disconnect (); int currentPartSize = fileSize/threadNumber + 1; // The download file size of each thread object RandomAccessFile files = new RandomAccessFile (targetFile, rw); files. setLength (fileSize); // set the file size files. close (); // close the file for (int I = 0; I
Import java. io. inputStream; import java. io. randomAccessFile; import java.net. httpURLConnection; import java.net. URL; public class DownThread extends Thread {private int start; // the start position of the download Thread, private int currentPartSize; // the size of the file downloaded by this Thread, private RandomAccessFile; // private String path of the stored file; // private int length; // the size of the file downloaded by this thread is public DownThread (String path, int start, int currentPartSize, RandomAccessFile file) {this. start = start; this. currentPartSize = currentPartSize; this. file = file; this. path = path ;}@ Overridepublic void run () {URL url; try {url = new URL (path); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setRequestMethod (GET); conn. setRequestProperty (Accept-Language, zh-CN); conn. setRequestProperty (Charset, UTF-8); conn. setRequestProperty (Connection, Keep-Alive); InputStream in = conn. getInputStream (); in. skip (start); // skip the start byte. Each thread is only responsible for downloading its own file byte [] buffer = new byte [1024]; int hasRead = 0; while (length