Android中怎么實(shí)現(xiàn)文件下載功能

今天就跟大家聊聊有關(guān)Android中怎么實(shí)現(xiàn)文件下載功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

建昌網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),建昌網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為建昌近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的建昌做網(wǎng)站的公司定做!

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

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

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("."));
 //打開手機(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ò)連接

  • 本地磁盤創(chuàng)建相同大小的空文件

  • 計(jì)算每條線程需從文件哪個(gè)部分開始下載,結(jié)束

  • 依次創(chuàng)建,啟動(dòng)多條線程來下載網(wǎng)絡(luò)資源的指定部分

public class Downloader {
 //添加@Test標(biāo)記是表示該方法是Junit測試的方法,就可以直接運(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");
   //獲得需要下載的文件的長度(大小)
   int filelength = conn.getContentLength();
   System.out.println("要下載的文件長度"+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è)位置開始下載
    int startposition = i * threadlength;
    //從文件的什么位置開始寫入數(shù)據(jù)
    RandomAccessFile threadfile = new RandomAccessFile(filename, "rwd");
    threadfile.seek(startposition);
    //啟動(dòng)三條線程分別從startposition位置開始下載文件
    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");
    //指定從什么位置開始下載
    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ì)下載的長度
      length += len;
     }
     threadfile.close();
     is.close();
     System.out.println("線程"+(threadid+1) + "已下載完成");
    }
   }catch(Exception ex){System.out.println("線程"+(threadid+1) + "下載出錯(cuò)"+ ex);}
  }
  
 }
}

看完上述內(nèi)容,你們對(duì)Android中怎么實(shí)現(xiàn)文件下載功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站名稱:Android中怎么實(shí)現(xiàn)文件下載功能
本文網(wǎng)址:http://muchs.cn/article46/jcjchg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航動(dòng)態(tài)網(wǎng)站、網(wǎng)站排名服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司