1 package com.thread; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.RandomAccessFile; 7 import java.net.HttpURLConnection; 8 import java.net.URL; 9 10 public class MultiDownLoad { 11 private final static int threadCount = 4; 12 13 public static void main(String[] args) throws Exception { 14 URL url = new URL("http://ubmcmm.baidustatic.com/media/v1/0f000Qk-RgkVYN5NV_NaO6.jpg"); 15 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 16 long contentLength = conn.getContentLengthLong(); 17 long perLength = contentLength/threadCount; 18 File file = new File("E:/123.jpg"); 19 //为每个线程分配一个随机写入流 20 RandomAccessFile[] raf = new RandomAccessFile[threadCount]; 21 InputStream[]is = new InputStream[threadCount]; 22 for(int i=0;i