importjava.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
/**
     實現思路:將檔案分成 n塊,每塊用不同執行緒去下載,


 * 檔案下載工具 by sam on 2015/11/5.
 */
public final class FileUtil {

    /**
     * 單執行緒下載檔案
     *
     * @param url       檔案的網路地址
     * @param path      儲存的檔案路徑
     * @param threadNum 想要開啟的執行緒數
     */
public static void dowanload(String url, String path, int threadNum)
            throws IOException {
        System.out.println("下載中...");
InputStream inputStream = null;
RandomAccessFile randomAccessFile = null;
        try {
            HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(10 * 1000);
File file = new File(path);
            if (!file.getParentFile().exists())
                file.getParentFile().mkdir();
            if (file.exists())
                file.delete();
file.createNewFile();
            int responseCode = urlConnection.getResponseCode();
            if (responseCode >= 200 && responseCode < 300) {
                int fileSize = urlConnection.getContentLength();//獲取檔案大小
randomAccessFile = new RandomAccessFile(file, "rwd");
randomAccessFile.setLength(fileSize);//設定檔案大小
randomAccessFile.close();//設定檔案大小後關閉
                //獲取執行緒要處理的塊數
int value = fileSize % threadNum;
                int unit = fileSize/threadNum;//檔案平均每塊大小
int block = 0 == value ? threadNum: threadNum + 1; //如果不整除,分多一塊處理
for (int i = 0; i < block; i++) {
                    int startMark = i * unit;//開始下載位置
int endMark = (i + 1) * unit - 1;//下載末端位置
DownloadFileThread thread = new DownloadFileThread(url, file, startMark, endMark);
thread.start();
}

            } else {
                System.out.println("伺服器異常...");
}
        } finally {
            if (null != inputStream) {
                inputStream.close();
}
            if (null != randomAccessFile) {
                randomAccessFile.close();
}
        }
    }

    /***
     * 下載檔案塊執行緒
     */
private static class DownloadFileThread extends Thread {
        private String url;
        private File file;
        private int startMark;
        private int endMark;
        public DownloadFileThread(String url, File file, int startMark, int endMark) {
            this.url = url;
            this.file = file;
            this.startMark = startMark;
            this.endMark = endMark;
}


        @Override
public void run() {
            InputStream inputStream = null;
RandomAccessFile randomAccessFile = null;
            try {
                HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(10 * 1000);
//請求指定檔案的位置,表明當前的一塊獲取的開始下載位置到末端位置
urlConnection.setRequestProperty("Range", "bytes="+startMark+"-"+endMark);
                int responseCode = urlConnection.getResponseCode();
                if (responseCode >= 200 && responseCode < 300)
                {
                    inputStream = urlConnection.getInputStream();
                    int len = 0;
                    byte[] data = new byte[4096];
randomAccessFile = new RandomAccessFile(file, "rwd");
randomAccessFile.seek(startMark);//設定往檔案寫入部分
while (-1 != (len = inputStream.read(data))) {
                        randomAccessFile.write(data, 0, len);
}
                    System.out.println("下載完成..."+getName());
}
                else
{
                    System.out.println("伺服器異常...");
}
            } catch (Exception ex) {

            }
            finally {
                if (null != inputStream) {
                    try {
                        inputStream.close();
} catch (IOException e) {
                        e.printStackTrace();
}
                }
                if (null != randomAccessFile) {
                    try {
                        randomAccessFile.close();
} catch (IOException e) {
                        e.printStackTrace();
}
                }
            }

        }
    }


    public static void main(String[] args) throws IOException {
        String path = "D:\\abc\\abc.jpg";
String url = "http://www.dowei.com/d/file/mingxing/bagua/20151105/9e88df8cd5dd243b31eff7a4f7d53f89.jpg";
FileUtil.dowanload(url, path, 4);
}
}
.