The implementation principle of multi‑thread download and resumable data transfer ., Multi-thread resumable upload

Source: Internet
Author: User

The implementation principle of multi‑thread download and resumable data transfer ., Multi-thread resumable upload

Package com. zzw. constant; public class Contant {public static int threadCount = 3;} 1 package com. zzw. download; 2 3 import java. io. bufferedReader; 4 import java. io. file; 5 import java. io. fileInputStream; 6 import java. io. fileOutputStream; 7 import java. io. IOException; 8 import java. io. inputStream; 9 import java. io. inputStreamReader; 10 import java. io. randomAccessFile; 11 import java.net. httpURLConn Ection; 12 import java.net. malformedURLException; 13 import java.net. URL; 14 import java.net. URLConnection; 15 16 import javax. tools. fileObject; 17 18 import com. zzw. constant. contant; 19 20 public class TestDownload 21 {22 // public static int threadCount = 3; 23 public static int blockSize = 0; 24 25 public static void main (String [] args) throws Exception 26 {27 URL url = new URL ("http: // localho St: 8080/download/cc.zip "); 28 29 HttpURLConnection conn = (HttpURLConnection) url. openConnection (); 30 conn. setRequestMethod ("GET"); 31 conn. setReadTimeout (10000); 32 33 long length = conn. getContentLength (); 34 35 conn. connect (); // connect 36 File file = new File ("cc.zip"); 37 38 RandomAccessFile randomFile = new RandomAccessFile (file, "rwd"); 39 randomFile. setLength (length); 40 41 // The download size of each thread is 42 S Ystem. out. println ("total size:" + length); 43 blockSize = (int) (length/Contant. threadCount); 44 System. out. println ("Download size of each thread:" + blockSize); 45 long start = 1; 46 long end = 0; 47 for (int I = 1; I <(Contant. threadCount + 1); I ++) 48 {49 50 // 1> 0-46354938 123456 51 // 2> 46354939-(46354939 + 46354938) 52 // 3> 53 start = blockSize * (I-1); 54 end = blockSize * I-1; 55 if (I = Contant. threadCoun T) 56 {57 end = length; 58} 59 System. out. println ("the" + I + "thread download interval:" + start + "-" + end); 60 new downloadThread (file, I, start, end ). start (); 61} 62 63 conn. disconnect (); 64} 65} 66 67 class downloadThread extends Thread 68 {69 private File path; // address of the local disk 70 private String url = "http: // localhost: 8080/download/cc.zip "; // the address of the HTTP request 71 private Integer threadID; // The number of threads currently 72 private lo Ng start; // start position of the thread to download 73 private long end; // end position of the thread 74 75 public downloadThread (File path, Integer threadID, long start, 76 long end) {77 this. path = path; 78 this. threadID = threadID; 79 this. start = start; 80 this. end = end; 81 82} 83 84 @ Override 85 public void run () 86 {87 try {88 System. out. println (threadID + "Enable Read"); 89 File contextFile = new File (threadID + ". txt "); 90 if (! ContextFile. exists () 91 {92 contextFile. createNewFile (); 93} 94 95 // read the data in the disconnection information to re-specify where to start writing 96 BufferedReader reader = new BufferedReader (new InputStreamReader (new FileInputStream (contextFile ))); 97 String info = reader. readLine (); 98 if (null! = Info & info. length ()> 0) 99 {100 start = start + Integer. parseInt (info); 101} 102 103 System. out. println ("start ........ = "+ start); 104 URL uri = new URL (url); 105 HttpURLConnection conn = (HttpURLConnection) uri. openconnections (); 106 conn. setRequestProperty ("Range", "bytes =" + start + "-" + end); 107 conn. setReadTimeout (50000); 108 conn. setConnectTimeout (50000); 109 110 conn. connect (); 111 112 // make the http request only Request a segment 113 System. out. println ("bytes =" + start + "-" + end); 114 115 200 // determine if it is? 116 int status = conn. getResponseCode (); 117 118 if (status/100 = 2) 119 {120 int total = 0; // The total number of bytes downloaded is 121. // FileOutputStream out = new FileOutputStream (new File (threadID + ". txt "); unreliable 122 RandomAccessFile random = new RandomAccessFile (path," rw "); 123 124 random. seek (start); 125 InputStream in = conn. getInputStream (); 126 byte [] buffer = new byte [1024]; 127 int len = 0; 128 129 while (len = in. read (bu Ffer ))! =-1) 130 {131 RandomAccessFile duandian = new RandomAccessFile (contextFile, "rwd"); 132 random. write (buffer, 0, len); 133 total + = len; 134 duandian. write (String. valueOf (total ). getBytes (); 135 duandian. close (); 136} 137 in. close (); 138 random. close (); 139} 140 conn. disconnect (); 141 System. out. println (threadID + "read completed"); 142} catch (MalformedURLException e) {143 e. printStackTrace (); 144} catch (IOException e) {145 e. printStackTrace (); 146} finally147 {148 synchronized (TestDownload. class) 149 {150 Contant. threadCount --; 151 System. out. println ("Contant. threadCount = "+ Contant. threadCount); 152 if (Contant. threadCount = 0) 153 {154 for (int I = 1; I <4; I ++) 155 {156 File contextFile = new File (I + ". txt "); 157 System. out. println (contextFile. delete () + "ccc"); 158} 159} 160} 161} 162} 163}

 

Related Article

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.