国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android文件下載功能實現代碼

2019-10-22 18:33:05
字體:
來源:轉載
供稿:網友

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

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

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

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(".")); //打開手機對應的輸出流,輸出到文件中 OutputStream os = context.openFileOutput("Cache_"+System.currentTimeMillis()+end, Context.MODE_PRIVATE); byte[] buffer = new byte[1024]; int len = 0; //從輸入六中讀取數據,讀到緩沖區中 while((len = is.read(buffer)) > 0) {  os.write(buffer,0,len); } //關閉輸入輸出流 is.close(); os.close();}

2.普通多線程下載:

步驟:

  • 獲取網絡連接
  • 本地磁盤創建相同大小的空文件
  • 計算每條線程需從文件哪個部分開始下載,結束
  • 依次創建,啟動多條線程來下載網絡資源的指定部分
public class Downloader { //添加@Test標記是表示該方法是Junit測試的方法,就可以直接運行該方法了  @Test  public void download() throws Exception  {   //設置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);   //生成一個大小相同的本地文件   RandomAccessFile file = new RandomAccessFile(filename, "rwd");   file.setLength(filelength);   file.close();   conn.disconnect();   //設置有多少條線程下載   int threadsize = 3;   //計算每個線程下載的量   int threadlength = filelength % 3 == 0 ? filelength/3:filelength+1;   for(int i = 0;i < threadsize;i++)   {    //設置每條線程從哪個位置開始下載    int startposition = i * threadlength;    //從文件的什么位置開始寫入數據    RandomAccessFile threadfile = new RandomAccessFile(filename, "rwd");    threadfile.seek(startposition);    //啟動三條線程分別從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);      //計算累計下載的長度      length += len;     }     threadfile.close();     is.close();     System.out.println("線程"+(threadid+1) + "已下載完成");    }   }catch(Exception ex){System.out.println("線程"+(threadid+1) + "下載出錯"+ ex);}  }   }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴海县| 镇宁| 泽普县| 洞口县| 祥云县| 南汇区| 九龙县| 米泉市| 睢宁县| 石台县| 桂东县| 淮阳县| 桂林市| 三门峡市| 峨眉山市| 扶风县| 浪卡子县| 尖扎县| 西平县| 新泰市| 永定县| 左权县| 瑞丽市| 洞头县| 丘北县| 东海县| 马尔康县| 林甸县| 盐池县| 南昌县| 钦州市| 高雄县| 临夏市| 江安县| 阳谷县| 大名县| 平昌县| 察隅县| 嘉峪关市| 阿瓦提县| 安吉县|