第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Android斷點(diǎn)續(xù)傳原理及實(shí)現(xiàn)

標(biāo)簽:
Android

原理:利用HttpURLConnection进行从网站上下载文件时,我们可以利用HttpURLConnection的setRequestProperty()方法,对我们要读取的字节部分进行控制,比如:

1.Range=0-100代表只读取前100个字节。
2.Range=100-500代表读取从第100个字节开始,读到第500个字节为止。
3.Range=100-则代表从第100个字节开始读取,一直读取到文件末尾结束。

这样我们就可以轻松的实现对文件的各部分的读取了,我们只需要在暂停时记录一下已经读取到的位置,在重新开始的时候利用setRequestProperty()方法设置一下我们要读取的字节位置即可。

这里还有一个问题就是我们应该怎样将读取到的字节流写入文件中,由于File是不支持在指定处写入字节的,因此我们这里要使用RandomAccessFile来进行字节流的写入,RandomAccessFile有一个方法seek(long),允许我们指定要写入的位置。

这样两者结合,就能够轻易的实现文件的断点续传了。

多线程断点续传实现原理相同,只是利用多个线程同时下载,每个线程指定要下载的字节部分,写入到文件的指定部分。故这里只给出了单线程断点续传下载的实现部分:

class SingleDownloadTask{

        private String filePath;
        private int contentLength;
        private int readLength;
        private RandomAccessFile file=null;
         private boolean isPause=false;
        private URL url;

         public boolean isDownloading() {
             return isDownloading;
         }

         private boolean isDownloading=false;

        private Handler mHandler=new Handler();

        public SingleDownloadTask(String urlStr,String filePath) {
            this.filePath=filePath;
            readLength=0;
            contentLength=0;
            try {
                url=new URL(urlStr);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }

        //download from pos
        public void download(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    isDownloading=true;
                    HttpURLConnection conn=null;
                    InputStream is=null;
                    BufferedInputStream bis=null;
                    try {
                        file=new RandomAccessFile(filePath,"rw");
                        file.seek(readLength);
                        conn= (HttpURLConnection) url.openConnection();
                        if(readLength==0){
                            contentLength=conn.getContentLength();
                        }else{
                            conn.setRequestProperty("Range","bytes="+readLength+"-");
                        }
                        is=conn.getInputStream();
                        bis=new BufferedInputStream(is);
                        byte[] bytes=new byte[1024];
                        int len=0;
                        while (!isPause&&((len=bis.read(bytes,0,1024)))!=-1){
                            file.write(bytes,0,len);
                            readLength+=len;
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    float rate=((float)readLength)/contentLength;
                                    mProgressBar.setProgress((int) (100*rate));
                                    mTextView.setText((int)(100*rate)+"%");
                                }
                            });
                        }
                        isDownloading=false;
                        if (readLength>=contentLength){
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "文件下载成功,保存在"+filePath
                                            , Toast.LENGTH_SHORT).show();
                                    mImageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
                                }
                            });
                        }
                        file.close();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        if (bis!=null){
                            try {
                                bis.close();
                                if (is!=null){
                                    is.close();
                                }
                                if (conn!=null){
                                    conn.disconnect();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }).start();
        }

        public void start(){
            isPause=false;
            download();
        }

        public void pause(){
            isPause=true;
        }

    }

完整的Demo地址

Demo:

原文链接:http://www.apkbus.com/blog-719059-62995.html

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消