view plain
  1. /**
  2. *断点续传
  3. *@paramurl
  4. */
  5. privatevoidduanDianXuChuan(finalStringurl){
  6. try{
  7. Runnabler=newRunnable(){
  8. @Override
  9. publicvoidrun(){
  10. getRemoteFile(url);
  11. }
  12. };
  13. newThread(r).start();
  14. }catch(Exceptione){
  15. e.printStackTrace();
  16. }
  17. }
  18. /**
  19. *下载远端文件
  20. *@paramurlString
  21. */
  22. privatevoidgetRemoteFile(StringurlString){
  23. try{
  24. Filefile;
  25. if(mCurSize==0){
  26. file=File.createTempFile(fileNa,fileEx);
  27. currentTempFilePath=file.getAbsolutePath();
  28. Log.e(“andy”,file.getName()+”hasexists.”);
  29. }else{
  30. file=newFile(currentTempFilePath);
  31. FileInputStreamfis=newFileInputStream(file);
  32. mCurSize=fis.available();
  33. Log.e(“andy”,file.getName()+”has”+mCurSize+”bytes.”);
  34. }
  35. URLurl=newURL(urlString);
  36. HttpURLConnectionhttpConnection=(HttpURLConnection)url
  37. .openConnection();
  38. httpConnection.setRequestProperty(“User-Agent”,“NetFox”);
  39. StringsProperty=“bytes=”+mCurSize+“-”;
  40. httpConnection.setRequestProperty(“RANGE”,sProperty);
  41. InputStreamis=httpConnection.getInputStream();
  42. Log.e(“andy”,”mTempFileName=”+mTempFileName);
  43. /*将文件写入暂存盘*/
  44. FileOutputStreamfos=newFileOutputStream(file,true);
  45. bytebuf[]=newbyte[512];
  46. do
  47. {
  48. intnumread=is.read(buf);
  49. if(numread<=0)
  50. {
  51. mHandler.sendEmptyMessageDelayed(this.DOWNLOAD_FINISH,0);
  52. break;
  53. }
  54. mCurSize+=numread;
  55. mHandler.sendEmptyMessageDelayed(this.UPDATE_DOWNLOAD_PROGRESS,0);
  56. Log.e(“andy”,”currentsize=”+mCurSize);
  57. fos.write(buf,0,numread);
  58. }while(mContinue);
  59. /*打开文件进行安装*/
  60. if(mCurSize==mSize){
  61. mCurSize=0;
  62. openFile(file);
  63. }
  64. try
  65. {
  66. is.close();
  67. }
  68. catch(Exceptionex)
  69. {
  70. Log.e(“andy”,“error:”+ex.getMessage(),ex);
  71. }
  72. }catch(Exceptione){
  73. e.printStackTrace();
  74. }
  75. }
  76. /**
  77. *获得文件长度
  78. *@paramurlString
  79. *@return
  80. */
  81. publiclonggetFileSize(StringurlString){
  82. intnFileLength=-1;
  83. try{
  84. URLurl=newURL(urlString);
  85. HttpURLConnectionhttpConnection=(HttpURLConnection)url
  86. .openConnection();
  87. httpConnection.setRequestProperty(“User-Agent”,“NetFox”);
  88. intresponseCode=httpConnection.getResponseCode();
  89. if(responseCode>=400){
  90. return-2;//-2representaccessiserror
  91. }
  92. StringsHeader;
  93. for(inti=1;;i++){
  94. sHeader=httpConnection.getHeaderFieldKey(i);
  95. if(sHeader!=null){
  96. if(sHeader.equals(“content-length”)){
  97. nFileLength=Integer.parseInt(httpConnection
  98. .getHeaderField(sHeader));
  99. break;
  100. }
  101. }else
  102. break;
  103. }
  104. }catch(IOExceptione){
  105. e.printStackTrace();
  106. }catch(Exceptione){
  107. e.printStackTrace();
  108. }
  109. returnnFileLength;
  110. }
  111. /**
  112. *计算百分比
  113. *@paramp1
  114. *@paramp2
  115. *@return
  116. */
  117. publicstaticStringpercent(doublep1,doublep2){
  118. Stringstr;
  119. doublep3=p1/p2;
  120. NumberFormatnf=NumberFormat.getPercentInstance();
  121. nf.setMinimumFractionDigits(2);
  122. str=nf.format(p3);
  123. Log.e(“andy”,”percentage=”+str);
  124. returnstr;
  125. }

更多相关文章

  1. Android(安卓)读取文件内容
  2. android中使用svg图标
  3. android 状态选择器
  4. android 报错 Proguard returned with error code 1 .
  5. Qt5.9.4搭建Android开发环境
  6. Android(安卓)读取doc文件
  7. Android(安卓)开发佳站3
  8. Android(安卓)插件化分析(5)- 加载外部dex
  9. Android(安卓)debug 问题集合

随机推荐

  1. Android(安卓)Studio下Intent隐式启动,发
  2. android Activity关闭动画 附左右动画ani
  3. 关于Android(安卓)Studio的JCenter库上传
  4. Android(安卓)studio下解决NoActionbar的
  5. Android属性:所设属性值为何在重起后被清
  6. Android(安卓)Binder框架实现之bindServi
  7. Android(安卓)选择图片,加载图片,显示图片
  8. [译]Android开发之究竟使用哪个布局
  9. android 多线程 UI 入门
  10. Android(安卓)display架构分析(5-8)