<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                上周時間基本都在做自己的項目了,所以很少來寫博客了。今天上來更新下博客,內容是多線程下載,以冒一下泡。 多線程在一定程度下增快了下載的速度,但是最終下載速度還是決定于你的帶寬,2G/3G網絡下“帶寬”這個詞是否可以忽略?。。 其實如果文章名字改為多線程斷點下載可能會屌一點,不過多線程下載和多線程斷點下載的區別幾乎只在于while循環的時候是否記錄已經下載的文件塊,斷點下載記錄了已經下載的文件塊所以下次下載就能從斷點開始下載(避免了重復下載),而下面這個代碼沒有記錄,所以不具備這個功能。 多線程下載主要是用到下面這幾個語句: ~~~ RandomAccessFile raf = new RandomAccessFile(PATH, "rwd"); raf.setLength(contentLength); ~~~ ~~~ conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); ~~~ ~~~ RandomAccessFile raf = new RandomAccessFile(PATH, "rwd"); raf.seek(startIndex); ~~~ 思路很簡單:首先獲得待下載文件的總長度,然后在本地生成一個一樣大小的臨時文件。然后將待下載的文件分塊,讓每個線程分別下載自己的文件塊。非常要注意的是文件的下標。 ~~~ package com.example.mytest; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; /** * 該類會開三個線程下載URI的內容,下載到PATH文件夾內 * * @author Hehyu * */ public class Main extends Activity { private final String PATH = Environment.getExternalStorageDirectory() + "/finger/東京暗鴉.jpg"; private final String URI = "http://e.hiphotos.baidu.com/zhidao/pic/item/e850352ac65c1038a7b590ceb0119313b17e89f7.jpg"; private final int THREAD_COUNT = 3; private int contentLength = -1; private int blockSize = -1; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { for (int threadId = 1; threadId <= THREAD_COUNT; threadId++) { int startIndex = (threadId - 1) * blockSize; int endIndex = threadId * blockSize - 1; if (threadId == THREAD_COUNT) { endIndex = contentLength; } System.out .println(threadId + " " + startIndex + " " + endIndex); // 開啟一個線程進行下載 new downloadThread(threadId, startIndex, endIndex).start(); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); new calSize().start(); } /** * 該線程專門負責計算文件總共長度,并且在本地生成一個同名同大小的文件 * * @author Hehyu * */ private class calSize extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); URL url; try { url = new URL(URI); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); // 獲取待下載文件長度 contentLength = conn.getContentLength(); blockSize = contentLength / THREAD_COUNT; RandomAccessFile raf = new RandomAccessFile(PATH, "rwd"); raf.setLength(contentLength); raf.close(); conn.disconnect(); // 已經獲取了文件長度,通知主線程去創建子線程下載文件 Message msg = Message.obtain(); handler.sendMessage(msg); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 專門負責下載的進程 * * @author Hehyu * */ private class downloadThread extends Thread { private int threadId; private int startIndex; private int endIndex; public downloadThread(int threadId, int startIndex, int endIndex) { // TODO Auto-generated constructor stub this.threadId = threadId; this.startIndex = startIndex; this.endIndex = endIndex; } @Override public void run() { // TODO Auto-generated method stub super.run(); try { RandomAccessFile raf = new RandomAccessFile(PATH, "rwd"); raf.seek(startIndex); URL url = new URL(URI); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); int code = conn.getResponseCode(); InputStream is = conn.getInputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { raf.write(buffer, 0, len); } raf.close(); conn.disconnect(); System.out.println("線程" + threadId + "下載完畢.."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看