Android断点续传核心代码

/**
 * 断 点 续 传
 * 
 * @param url
 */
private void duanDianXuChuan(final String url) {
    try {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                getRemoteFile(url);
            }
        };
        new Thread(r).start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 下载远端文件
 * 
 * @param urlString
 */
private void getRemoteFile(String urlString){
    try{
        File file;
        if(mCurSize == 0){
            file = File.createTempFile(fileNa, fileEx);
            currentTempFilePath = file.getAbsolutePath();
            Log.e("andy",file.getName() + " has exists.");
        } else {
            file = new File(currentTempFilePath);
            FileInputStream fis = new FileInputStream(file);
            mCurSize = fis.available();
            Log.e("andy",file.getName() + " has " + mCurSize + " bytes.");
        }
        URL url = new URL(urlString);
        HttpURLConnection httpConnection = (HttpURLConnection) url
        .openConnection();
        httpConnection.setRequestProperty(“User-Agent”, “NetFox”);
        String sProperty = "bytes=" + mCurSize + "-";
        httpConnection.setRequestProperty("RANGE", sProperty);
        InputStream is = httpConnection.getInputStream();
        Log.e("andy","mTempFileName = " + mTempFileName);
        
        FileOutputStream fos = new FileOutputStream(file, true);
        byte buf[] = new byte[512];
        do
        {
            int numread = 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","current="" size=" + mCurSize);
            fos.write(buf, 0, numread);
        }while (mContinue);
        
        if(mCurSize == mSize) {
            mCurSize = 0;
            openFile(file);
        }
    try
    {
        is.close();
    }
    catch (Exception ex)
    {
        Log.e(" andy",="" "error:="" "="" +="" ex.getmessage(),="" ex);="" }="" }catch(exception="" e){="" e.printstacktrace();="" }="" }="" *="" *="" 获得文件长度="" *="" *="" @param="" urlstring="" *="" @return="" */="" public="" long="" getfilesize(string="" urlstring)="" {="" int="" nfilelength="-1;" try="" {="" url="" url="new" url(urlstring);="" httpurlconnection="" httpconnection="(HttpURLConnection)" url="" .openconnection();="" httpconnection.setrequestproperty("user-agent",="" "netfox");="" int="" responsecode="httpConnection.getResponseCode();" if="" (responsecode="">= 400) {
        return -2; // -2 represent access is error
    }
    String sHeader;
    for (int i = 1;; i++) {
        sHeader = httpConnection.getHeaderFieldKey(i);
        if (sHeader != null) {
            if (sHeader.equals("content-length")) {
                nFileLength = Integer.parseInt(httpConnection
                .getHeaderField(sHeader));
                break;
            }
        } else
            break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return nFileLength;
}

/**
 * 计算百分比
 * 
 * @param p1
 * @param p2
 * @return
 */
public static String percent(double p1, double p2) {
    String str;
    double p3 = p1 / p2;
    NumberFormat nf = NumberFormat.getPercentInstance();
    nf.setMinimumFractionDigits(2);
    str = nf.format(p3);
    Log.e("andy","percentage = " + str);
    return str;
}
暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心