Android断点续传核心代码
16lz
2021-01-24
view plain
- /**
- *断点续传
- *@paramurl
- */
- privatevoidduanDianXuChuan(finalStringurl){
- try{
- Runnabler=newRunnable(){
- @Override
- publicvoidrun(){
- getRemoteFile(url);
- }
- };
- newThread(r).start();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- /**
- *下载远端文件
- *@paramurlString
- */
- privatevoidgetRemoteFile(StringurlString){
- try{
- Filefile;
- if(mCurSize==0){
- file=File.createTempFile(fileNa,fileEx);
- currentTempFilePath=file.getAbsolutePath();
- Log.e(“andy”,file.getName()+”hasexists.”);
- }else{
- file=newFile(currentTempFilePath);
- FileInputStreamfis=newFileInputStream(file);
- mCurSize=fis.available();
- Log.e(“andy”,file.getName()+”has”+mCurSize+”bytes.”);
- }
- URLurl=newURL(urlString);
- HttpURLConnectionhttpConnection=(HttpURLConnection)url
- .openConnection();
- httpConnection.setRequestProperty(“User-Agent”,“NetFox”);
- StringsProperty=“bytes=”+mCurSize+“-”;
- httpConnection.setRequestProperty(“RANGE”,sProperty);
- InputStreamis=httpConnection.getInputStream();
- Log.e(“andy”,”mTempFileName=”+mTempFileName);
- /*将文件写入暂存盘*/
- FileOutputStreamfos=newFileOutputStream(file,true);
- bytebuf[]=newbyte[512];
- do
- {
- intnumread=is.read(buf);
- if(numread<=0)
- {
- mHandler.sendEmptyMessageDelayed(this.DOWNLOAD_FINISH,0);
- break;
- }
- mCurSize+=numread;
- mHandler.sendEmptyMessageDelayed(this.UPDATE_DOWNLOAD_PROGRESS,0);
- Log.e(“andy”,”currentsize=”+mCurSize);
- fos.write(buf,0,numread);
- }while(mContinue);
- /*打开文件进行安装*/
- if(mCurSize==mSize){
- mCurSize=0;
- openFile(file);
- }
- try
- {
- is.close();
- }
- catch(Exceptionex)
- {
- Log.e(“andy”,“error:”+ex.getMessage(),ex);
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- /**
- *获得文件长度
- *@paramurlString
- *@return
- */
- publiclonggetFileSize(StringurlString){
- intnFileLength=-1;
- try{
- URLurl=newURL(urlString);
- HttpURLConnectionhttpConnection=(HttpURLConnection)url
- .openConnection();
- httpConnection.setRequestProperty(“User-Agent”,“NetFox”);
- intresponseCode=httpConnection.getResponseCode();
- if(responseCode>=400){
- return-2;//-2representaccessiserror
- }
- StringsHeader;
- for(inti=1;;i++){
- sHeader=httpConnection.getHeaderFieldKey(i);
- if(sHeader!=null){
- if(sHeader.equals(“content-length”)){
- nFileLength=Integer.parseInt(httpConnection
- .getHeaderField(sHeader));
- break;
- }
- }else
- break;
- }
- }catch(IOExceptione){
- e.printStackTrace();
- }catch(Exceptione){
- e.printStackTrace();
- }
- returnnFileLength;
- }
- /**
- *计算百分比
- *@paramp1
- *@paramp2
- *@return
- */
- publicstaticStringpercent(doublep1,doublep2){
- Stringstr;
- doublep3=p1/p2;
- NumberFormatnf=NumberFormat.getPercentInstance();
- nf.setMinimumFractionDigits(2);
- str=nf.format(p3);
- Log.e(“andy”,”percentage=”+str);
- returnstr;
- }
更多相关文章
- Android(安卓)读取文件内容
- android中使用svg图标
- android 状态选择器
- android 报错 Proguard returned with error code 1 .
- Qt5.9.4搭建Android开发环境
- Android(安卓)读取doc文件
- Android(安卓)开发佳站3
- Android(安卓)插件化分析(5)- 加载外部dex
- Android(安卓)debug 问题集合