這篇文章將為大家詳細(xì)講解有關(guān)在Java項(xiàng)目中如何利用多線程實(shí)現(xiàn)文件下載功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
揚(yáng)中網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),揚(yáng)中網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為揚(yáng)中上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的揚(yáng)中做網(wǎng)站的公司定做!
具體內(nèi)容如下
import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class MulThreadDownload { public static void main(String[] args) throws Exception { String path = "http://192.168.1.100:8080/Hello/Big.exe"; new MulThreadDownload().download(path, 3); } /** * 下載文件 * * @param path * 網(wǎng)絡(luò)文件路徑 * @param threadSize * 線程數(shù) * @throws Exception */ private void download(String path, int threadSize) throws Exception { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); if (connection.getResponseCode() == 200) { int length = connection.getContentLength();// 獲取網(wǎng)絡(luò)文件長度 File file = new File(getFileName(path)); // 在本地生成一個(gè)長度與網(wǎng)絡(luò)文件相同的文件 RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); accessFile.setLength(length); accessFile.close(); // 計(jì)算每條線程負(fù)責(zé)下載的數(shù)據(jù)量 int block = length % threadSize == 0 ? length / threadSize : length / threadSize + 1; for (int threadId = 0; threadId < threadSize; threadId++) { new DownloadThread(threadId, block, url, file).start(); } } else { System.out.println("download fail"); } } private class DownloadThread extends Thread { private int threadId; private int block; private URL url; private File file; public DownloadThread(int threadId, int block, URL url, File file) { this.threadId = threadId; this.block = block; this.url = url; this.file = file; } @Override public void run() { int start = threadId * block; // 計(jì)算該線程從網(wǎng)絡(luò)文件什么位置開始下載 int end = (threadId + 1) * block - 1; // 計(jì)算下載到網(wǎng)絡(luò)文件什么位置結(jié)束 try { RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); accessFile.seek(start); //從start開始 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); //設(shè)置獲取資源數(shù)據(jù)的范圍,從start到end connection.setRequestProperty("Range", "bytes=" + start + "-" + end); //注意多線程下載狀態(tài)碼是 206 不是200 if (connection.getResponseCode() == 206) { InputStream inputStream = connection.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { accessFile.write(buffer, 0, len); } accessFile.close(); inputStream.close(); } System.out.println("第" + (threadId + 1) + "條線程已經(jīng)下載完成"); } catch (Exception e) { e.printStackTrace(); } } } /** * 獲取文件名稱 * * @param path * 網(wǎng)絡(luò)文件路徑 * @return */ private String getFileName(String path) { return path.substring(path.lastIndexOf("/") + 1); } }
關(guān)于在Java項(xiàng)目中如何利用多線程實(shí)現(xiàn)文件下載功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。