最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android文件下載功能實(shí)現(xiàn)代碼

 更新時(shí)間:2021年07月26日 09:37:08   作者:yl007  
這篇文章主要為大家詳細(xì)介紹了Android文件下載功能實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android文件下載功能的具體代碼,供大家參考,具體內(nèi)容如下

1.普通單線程下載文件:

直接使用URLConnection.openStream()打開(kāi)網(wǎng)絡(luò)輸入流,然后將流寫(xiě)入到文件中!

public static void downLoad(String path,Context context)throws Exception
{
 URL url = new URL(path);
 InputStream is = url.openStream();
 //截取最后的文件名
 String end = path.substring(path.lastIndexOf("."));
 //打開(kāi)手機(jī)對(duì)應(yīng)的輸出流,輸出到文件中
 OutputStream os = context.openFileOutput("Cache_"+System.currentTimeMillis()+end, Context.MODE_PRIVATE);
 byte[] buffer = new byte[1024];
 int len = 0;
 //從輸入六中讀取數(shù)據(jù),讀到緩沖區(qū)中
 while((len = is.read(buffer)) > 0)
 {
  os.write(buffer,0,len);
 }
 //關(guān)閉輸入輸出流
 is.close();
 os.close();
}

2.普通多線程下載:

步驟:

  • 獲取網(wǎng)絡(luò)連接
  • 本地磁盤(pán)創(chuàng)建相同大小的空文件
  • 計(jì)算每條線程需從文件哪個(gè)部分開(kāi)始下載,結(jié)束
  • 依次創(chuàng)建,啟動(dòng)多條線程來(lái)下載網(wǎng)絡(luò)資源的指定部分
public class Downloader {
 //添加@Test標(biāo)記是表示該方法是Junit測(cè)試的方法,就可以直接運(yùn)行該方法了
  @Test
  public void download() throws Exception
  {
   //設(shè)置URL的地址和下載后的文件名
   String filename = "meitu.exe";
   String path = "http://10.13.20.32:8080/Test/XiuXiu_Green.exe";
   URL url = new URL(path);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setConnectTimeout(5000);
   conn.setRequestMethod("GET");
   //獲得需要下載的文件的長(zhǎng)度(大小)
   int filelength = conn.getContentLength();
   System.out.println("要下載的文件長(zhǎng)度"+filelength);
   //生成一個(gè)大小相同的本地文件
   RandomAccessFile file = new RandomAccessFile(filename, "rwd");
   file.setLength(filelength);
   file.close();
   conn.disconnect();
   //設(shè)置有多少條線程下載
   int threadsize = 3;
   //計(jì)算每個(gè)線程下載的量
   int threadlength = filelength % 3 == 0 ? filelength/3:filelength+1;
   for(int i = 0;i < threadsize;i++)
   {
    //設(shè)置每條線程從哪個(gè)位置開(kāi)始下載
    int startposition = i * threadlength;
    //從文件的什么位置開(kāi)始寫(xiě)入數(shù)據(jù)
    RandomAccessFile threadfile = new RandomAccessFile(filename, "rwd");
    threadfile.seek(startposition);
    //啟動(dòng)三條線程分別從startposition位置開(kāi)始下載文件
    new DownLoadThread(i,startposition,threadfile,threadlength,path).start();
   }
   int quit = System.in.read();
   while('q' != quit)
   {
    Thread.sleep(2000);
   }
  }

 private class DownLoadThread extends Thread {
  private int threadid;
  private int startposition;
  private RandomAccessFile threadfile;
  private int threadlength;
  private String path;
  public DownLoadThread(int threadid, int startposition,
    RandomAccessFile threadfile, int threadlength, String path) {
   this.threadid = threadid;
   this.startposition = startposition;
   this.threadfile = threadfile;
   this.threadlength = threadlength;
   this.path = path;
  }
  public DownLoadThread() {}
  @Override
  public void run() {
   try
   {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5000);
    conn.setRequestMethod("GET");
    //指定從什么位置開(kāi)始下載
    conn.setRequestProperty("Range", "bytes="+startposition+"-");
    //System.out.println(conn.getResponseCode());
    if(conn.getResponseCode() == 206)
    {
     InputStream is = conn.getInputStream();
     byte[] buffer = new byte[1024];
     int len = -1;
     int length = 0;
     while(length < threadlength && (len = is.read(buffer)) != -1)
     {
      threadfile.write(buffer,0,len);
      //計(jì)算累計(jì)下載的長(zhǎng)度
      length += len;
     }
     threadfile.close();
     is.close();
     System.out.println("線程"+(threadid+1) + "已下載完成");
    }
   }catch(Exception ex){System.out.println("線程"+(threadid+1) + "下載出錯(cuò)"+ ex);}
  }
  
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Flutter軟鍵盤(pán)的原理淺析

    Flutter軟鍵盤(pán)的原理淺析

    大家應(yīng)該都知道目前Flutter官方是沒(méi)有自定義鍵盤(pán)的解決方案,下面這篇文章主要給大家介紹了關(guān)于Flutter軟鍵盤(pán)原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • Android實(shí)現(xiàn)簡(jiǎn)易記事本

    Android實(shí)現(xiàn)簡(jiǎn)易記事本

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易記事本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android Camera是否支持變焦的判斷方法總結(jié)

    Android Camera是否支持變焦的判斷方法總結(jié)

    這篇文章主要介紹了Android Camera是否支持變焦的判斷方法總結(jié),本文總結(jié)了調(diào)節(jié)攝像頭焦距編程中遇到的一些問(wèn)題和解決方法,需要的朋友可以參考下
    2015-04-04
  • Android Studio實(shí)現(xiàn)幀動(dòng)畫(huà)

    Android Studio實(shí)現(xiàn)幀動(dòng)畫(huà)

    這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)幀動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Android Notification通知使用詳解

    Android Notification通知使用詳解

    消息通知(Notification)是Android系統(tǒng)中比較有特色的一個(gè)功能,當(dāng)某個(gè)應(yīng)用程序希望用戶發(fā)出一些提示信息,而該應(yīng)用又不在前臺(tái)運(yùn)行時(shí),就可以借助通知來(lái)實(shí)現(xiàn)
    2022-09-09
  • OKHttp使用詳解

    OKHttp使用詳解

    OkHttp 是一套處理 HTTP 網(wǎng)絡(luò)請(qǐng)求的依賴(lài)庫(kù),由 Square 公司設(shè)計(jì)研發(fā)并開(kāi)源,目前可以在 Java 和 Kotlin 中使用,這篇文章主要介紹了OKHttp詳解,需要的朋友可以參考下
    2024-01-01
  • Android編程之非調(diào)用系統(tǒng)界面實(shí)現(xiàn)發(fā)送彩信的方法(MMS)

    Android編程之非調(diào)用系統(tǒng)界面實(shí)現(xiàn)發(fā)送彩信的方法(MMS)

    這篇文章主要介紹了Android編程之非調(diào)用系統(tǒng)界面實(shí)現(xiàn)發(fā)送彩信的方法,涉及Android源碼中的mms的使用技巧,需要的朋友可以參考下
    2016-01-01
  • Android Bluetooth藍(lán)牙技術(shù)使用流程詳解

    Android Bluetooth藍(lán)牙技術(shù)使用流程詳解

    這篇文章主要介紹了Android Bluetooth藍(lán)牙技術(shù)使用流程詳解的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Android中生成、使用Json數(shù)據(jù)實(shí)例

    Android中生成、使用Json數(shù)據(jù)實(shí)例

    這篇文章主要介紹了Android中生成、使用Json數(shù)據(jù)實(shí)例,本文直接給出了實(shí)現(xiàn)代碼,相對(duì)容易理解,需要的朋友可以參考下
    2014-10-10
  • Android 動(dòng)態(tài)的顯示時(shí)間

    Android 動(dòng)態(tài)的顯示時(shí)間

    本文給大家分享一段代碼實(shí)現(xiàn)android動(dòng)態(tài)顯示時(shí)間,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2016-12-12

最新評(píng)論

綦江县| 威海市| 黄浦区| 布尔津县| 临武县| 云林县| 体育| 喀喇沁旗| 蚌埠市| 永顺县| 靖州| 黄骅市| 普陀区| 体育| 高台县| 襄汾县| 闽清县| 东阿县| 长寿区| 眉山市| 汉寿县| 德安县| 建宁县| 安庆市| 石林| 海晏县| 佛坪县| 广汉市| 唐河县| 慈溪市| 广河县| 祁门县| 新晃| 广东省| 呼和浩特市| 河北区| 洛川县| 武陟县| 耿马| 临泉县| 清新县|