如何确定文件已完成下载

时间:2013-05-31 17:24:21

标签: java servlets http-get

我正在尝试通过确定下载固定大小文件所需的时间来计算网络带宽。

starttime=Calendar.getInstance().getTimeInMillis();
HttpResponse response=cts.getContent();
if(response != null) 
   endtime=Calendar.getInstance().getTimeInMillis();    

这是我的getContent()方法:

public HttpResponse getContent()
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet postRequest = new HttpGet("myserverurl");
        try {
             Log.i(tag,"before execute");
             HttpResponse response = httpClient.execute(postRequest);
              Log.i(tag,"after execute");
            HttpEntity httpEntity = response.getEntity();
            is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              Log.i(tag,sb.toString());
              response.toString();
              return response;

        } catch (Exception e) {
            Log.i(tag,"execption="+e);
            return null;
        }

    }

我计算此方法响应的结束时间。我的问题是什么时候是计算结束时间的合适时机?如果我在行之后立即采取结束时间:

HttpEntity httpEntity = response.getEntity();

我获得的带宽非常高,当我执行while循环后,带宽完全不同。

哪种方法是正确的?文件何时完全下载?

1 个答案:

答案 0 :(得分:0)

根据我的理解,它是同步的,反对异步,所以在存储响应之后就是下载结束的地方