• Last time, I went through the process and principles of the download, and it was obvious that there were a few key points: how to connect to network addresses, that is, servlets, IO streams, and thread pools.

fromHttpURLConnectionstart

HttpURLConnection is Java provides a basic class library to initiate HTTP requests, can be done to initiate POST GET requests, we just need to GET and set the request header to GET some necessary file information and some other operations, relatively simple.

HttpURLConnection encapsulation is relatively simple, many things need to be done by ourselves, but we are involved in the operation is not much, there is no so-called, the basic thing is used.

Statistics on the apis involved

  • Set the request header: setRequestProperty

  • Get the request header: getHeaderField

  • Connection timeout: setConnectTimeout

  • Get the status code: getResponseCode

Usage and basic flow

  1. The first step, of course, is to establish a connection

    URL url1 = new URL(url);// Get a URL object
    HttpURLConnection httpURLConnection = (HttpURLConnection) url1.openConnection();
    // Use the HttpURLConnection object to fetch web page data from the network
    Copy the code
  2. To avoid connection timeout, set the connection timeout limit. (HttpURLConnection is based on HTTP and its underlying communication is implemented through sockets. If you do not set timeout, in the case of network exceptions, the program may freeze and not proceed.

    httpURLConnection.setConnectTimeout(6*1000);// To avoid connection timeout
    Copy the code
  3. As we said earlier, we can set the content-range field. Content-range: a-b is downloaded from bytes A to bytes B

    if(...). { httpURLConnection.setRequestProperty("RANGE"."bytes=" + start + "-" + end);
    	} else {
        	httpURLConnection.setRequestProperty("RANGE"."bytes" + start + "-");
        }
    Copy the code
  4. And, of course, getting the size

    int length = httpURLConnection.getContentLength();
    Copy the code
  5. Get the request header to get the Etag

    Map<String, List<String>> headerFields = httpUrlConnection.getHeaderFields();
    List<String> eTagList = headerFields.get("ETag");
    Copy the code
  6. Close the connection

    httpUrlConnection.disconnect();
    Copy the code

IO streaming

try(InputStream inputStream = httpURLConnection.getInputStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    RandomAccessFile randomAccessFile = new RandomAccessFile(httpFileName,"rw")){
    randomAccessFile.seek(localFileContentLength);
    byte[] bytes = new byte[BYTE_SIZE];
    int len = -1;
    while((len = bufferedInputStream.read(bytes)) ! = -1){
        randomAccessFile.write(bytes,0,len); }}catch(...). {... }finally{... }Copy the code
  • This is the process of writing files through the IO stream
  • InputStream inputStream = httpURLConnection.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); RandomAccessFile randomAccessFile = new RandomAccessFile(httpFileName,"rw")

Then try(…) If {} is an exception, the resource in () will be closed. This is handy in inputStream and outputStream applications. After a search on the web: this is the so-called try-with-resources, which means the resources in parentheses are always closed at the end of the statement.

The Future model

Here is after studying ao third big man’s article, try to write come out. So isn’t Future just a man who cheats and plays with women’s feelings?

Nonsense not much said, direct AoBing bosses link on https://juejin.cn/post/6950065347227549703!

parameter

This is because the getContentLength() method kept returning -1 during the test.

Then after a short baidu, found that there seems to be a parameter problem did not pay attention to:

HttpConnection. SetRequestProperty (” the user-agent “, “Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36”);

If the user-agent is not set, it is equivalent to telling others that I want to climb you. So we can set this parameter a little bit