关于Android程序中java的HttpUrlConnection的getContentLength方法

时间:2017-01-13 08:14:42

标签: java android httpurlconnection

我可以下载.apk和其他可执行文件,除非我尝试通过公司WiFi进行。这是我的结果矩阵。

enter image description here

如果我没有为URL连接明确设置任何属性(使用setRequestProperty()),则getContentLength()将返回正确的长度。

但是,因为程序正在进行多线程下载,所以我必须使用setRequestProperty设置Range属性,如下所示:

Range: bytes = 0 - TotalBytes / TheadNum  //first thread
Range: bytes = TotalBytes/ThreadNum - TotalBytes / TheadNum * 2  //second thread, and etc

当我这样做时,我在上述形式中有四分之一的错误结果。

   threadNum          = 2
   totalFileSize      = 39058483
   getContentLength() = 171 // (expected correct value is 17217690)

0 个答案:

没有答案