Android文件下載功能實(shí)現(xiàn)代碼
本文實(shí)例為大家分享了Android文件下載功能的具體代碼,供大家參考,具體內(nèi)容如下
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)多條線程來(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è)位置開始下載
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ì)下載的長(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í)有所幫助,也希望大家多多支持腳本之家。
- Android文件下載進(jìn)度條的實(shí)現(xiàn)代碼
- Android zip文件下載和解壓實(shí)例
- Android實(shí)現(xiàn)文件下載進(jìn)度顯示功能
- Android 文件下載三種基本方式
- Android實(shí)現(xiàn)簡(jiǎn)單的文件下載與上傳
- Android Retrofit文件下載進(jìn)度顯示問(wèn)題的解決方法
- Android 將文件下載到指定目錄的實(shí)現(xiàn)代碼
- Android基于HttpUrlConnection類的文件下載實(shí)例代碼
- android實(shí)現(xiàn)文件下載功能
- Android簡(jiǎn)單實(shí)現(xiàn)文件下載
相關(guān)文章
android 捕獲系統(tǒng)異常并上傳日志具體實(shí)現(xiàn)
這篇文章介紹了android 捕獲系統(tǒng)異常并上傳日志具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-09-09
android利用ContentResolver訪問(wèn)者獲取手機(jī)短信信息
本篇文章主要介紹了android利用ContentResolver訪問(wèn)者獲取手機(jī)短信信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
Flutter打包apk報(bào)錯(cuò)Your?app?isn't?using?AndroidX解決
這篇文章主要為大家介紹了Flutter打包apk報(bào)錯(cuò)Your?app?isn't?using?AndroidX解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android開發(fā)之permission動(dòng)態(tài)權(quán)限獲取詳解
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之permission動(dòng)態(tài)權(quán)限獲取,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android啟動(dòng)畫面的實(shí)現(xiàn)方法
這篇文章主要介紹了Android啟動(dòng)畫面的實(shí)現(xiàn)方法,分析了布局文件及加載啟動(dòng)文件的實(shí)現(xiàn)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01
Android事件分發(fā)機(jī)制?ViewGroup分析
這篇文章主要介紹了Android事件分發(fā)機(jī)制?ViewGroup分析,事件分發(fā)從手指觸摸屏幕開始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會(huì)傳遞給Android的輸入系統(tǒng)服務(wù)IMS,更多相關(guān)介紹,需要的朋友可以參考一下2022-09-09
Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí)
這篇文章主要介紹了Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android源碼學(xué)習(xí)之工廠方法模式應(yīng)用及優(yōu)勢(shì)介紹
工廠方法模式定義:定義一個(gè)用于創(chuàng)建對(duì)象的接口,讓子類決定實(shí)例化哪一個(gè)類。工廠方法使一個(gè)類的實(shí)例化延遲到其子類,感興趣的朋友可以了解下哦2013-01-01
flutter開發(fā)的app項(xiàng)目?打包成web
如果你的Flutter版本低于2.0,請(qǐng)先升級(jí)Flutter版本,創(chuàng)建一個(gè)web文件夾來(lái)存放web相關(guān)的資源,使用HTML渲染器打包,該渲染器提供的打開速度最快,并且具有良好的瀏覽器兼容性,使用默認(rèn)設(shè)置進(jìn)行打包,提供的打開速度為一般,但依然保持良好的瀏覽器兼容性2024-08-08

