Latest --- multi-thread download of java files

Source: Internet
Author: User

Import java. io. inputStream; import java. io. randomAccessFile; import java.net. httpURLConnection; import java.net. URL; public class Demo {// define the number of threads public static int threadCount = 5; public static void main (String [] args) throws Exception {// 1, connect to the server, obtain a file. The file size is the same as that of the server. The temporary file String path = "http: // 172.22.64.193: 8080/0001 AndroidWebService/test.exe "; URL url = new URL (path); HttpURLConnection conn = (HttpUR LConnection) url. openConnection (); // set timeout conn. setConnectTimeout (5000); // sets the Request Method conn. setRequestMethod ("GET"); // GET the server's return code int code = conn. getResponseCode (); // judge the return code if (code = 200) {// obtain the returned length int length = conn. getContentLength (); System. out. println ("total file length:" + length); // create a temporary file RandomAccessFile raf = new RandomAccessFile ("temp.exe ", "rwd"); // specifies the size of the temporary file raf. setLength (length); // release the resource raf. cl Ose (); // average file size of each thread int blockSize = length/threadCount; for (int threadId = 1; threadId <= threadCount; threadId ++) {// download position starting from the thread int startIndex = (threadId-1) * blockSize; // The end position of the thread int endIndex = threadId * blockSize-1; // determine whether it is the last thread if (threadId = threadCount) {// set the end position to the end of the file endIndex = length;} System. out. println ("thread:" + threadId + "Download: >>>>>>>>>" + startIndex + ">>>>>>>>>" + endIndex ); new DownlodeThread (path, threadId, startIndex, endIndex ). start () ;}}/ *** subthread class for downloading files, each Thread downloads the file data at the corresponding location ** @ author MartinDong **/public static class DownlodeThread extends Thread {private String path; private int threadId; private int startIndex; private int endIndex; /***** @ param path * File Download path * @ param threadId * thread id * @ param startIndex * thread start position * @ param endIndex * thread end position */ public DownlodeThread (Stri Ng path, int threadId, int startIndex, int endIndex) {this. path = path; this. threadId = threadId; this. startIndex = startIndex; this. endIndex = endIndex ;}@ Overridepublic void run () {try {// convert the address to URLURL url = new URL (path); // obtain the http connection HttpURLConnection conn = (HttpURLConnection) url. openConnection (); // set the Connection Request Method conn. setRequestMethod ("GET"); // important: Requests the server to download part of the file, specifying the position of the file conn. setRequestProperty ("Range", "byt Es = "+ startIndex +"-"+ endIndex); // set the timeout value conn. setReadTimeout (5000); // get the server status code. The value 200 indicates that all requested resources are responded = OK, 206 response = okint code = conn for some requested resources. getResponseCode (); System. out. println ("code:" + code); // The returned file stream InputStream is = conn. getInputStream (); // create a temporary file named RandomAccessFile raf = new RandomAccessFile ("temp.exe", "rwd"); // move the pointer to the specified file location, raf. seek (startIndex); // creates an intermediate buffer byte array byte [] buffer = new byte [1024]; int length = 0; while (length = is. read (buffer ))! =-1) {raf. write (buffer, 0, length);} is. close (); raf. close (); System. out. println ("thread:" + threadId + "download completed ............ ");} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}

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.