检查下载进度

时间:2011-04-08 01:39:15

标签: android

我按照以下方式下载并且工作正常。但是想要检查每个块的来源。

文件文件=新文件(extStorageDirectory +“/”+ runInBackend.count + sche.file_name);

HttpURLConnection connection = (HttpURLConnection)url.openConnection();

   connection.setDoOutput(true);

   connection.setConnectTimeout(sche.TimeOut);

   connection.setRequestMethod("POST");

   String status = connection.getHeaderField(0);//getResponseMessage();

   if(status != null && status.contains("200"))

    {
            InputStream is = connection.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);

            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            long totalBytesRead = 0;
                 while ((current = bis.read()) != -1) 

                 {
                     baf.append((byte) current);

                     totalBytesRead ++;

                 }
                 FileOutputStream fos = new FileOutputStream(file);

                 fos.write(baf.toByteArray());

                 fos.close();

有没有办法检查任何fie的下载进度,即每个块的到来时间。

plzz帮助...

由于

1 个答案:

答案 0 :(得分:0)

您应该将下载移动到后台线程,例如,使用AsyncTask

当您获得初始响应时,您可以提取内容长度标题以跟踪您在下载中的位置。如果您想要生成ETA,则始终可以使用System.currentTimeMillisDate获取当前时间。

希望这有帮助,

Phil Lello