我正在一个 android 应用程序中进行下载,我需要获取 content_length 以显示下载的百分比。
这些文件位于我的客户设置的服务器上。在此服务器上,对 getContentLength() 的调用返回 -1。以下是标题:
THIS SERVERS HEADER DOESN'T WORK WITH urlConnection.getContentLength()
$ curl --head "http://www.server0.com/ango/thefile.m4v"
HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=A17762C6B65FD8D35840110F8628DE4B.V5; Path=/; HttpOnly
X-ServedBy: web114
Set-Cookie: SS_MID=7315b0cb-054c-4dd8-a013-c7163793becehfjcndg8; Domain=.tangoberretin.com; Expires=Thu, 13-Apr-2023 07:57:07 GMT; Path=/
Pragma: cache
Cache-Control: private,max-age=86400
Last-Modified: Thu, 21 Feb 2013 06:28:24 GMT
Content-Disposition: attachment; filename="thefile.m4v";
Accept-Ranges: bytes
Content-Range: bytes 0-6716578/6716579
Content-Type: application/octet-stream;charset=UTF-8
Content-Length: 6716579
Date: Mon, 15 Apr 2013 07:57:07 GMT
Server: SSWS
Set-Cookie: WebPersCookie=Gn5lN9r2EIK+3eKz0tv/bEs4IJpcBzYg22jKvv/JD1eAA1N3D0pevaz3zuXOD3uTT950AQdstGRJgdE=; path=/
所以我做了一个测试并将文件放在我自己的服务器(apache)上,一切正常。以下是这些标题:
THIS SERVERS HEADER WORKS WITH urlConnection.getContentLength()
$ curl --head "http://server1.net/wp-content/video/thefile.m4v"
HTTP/1.1 200 OK
Date: Mon, 15 Apr 2013 07:57:42 GMT
Server: Apache
Last-Modified: Sun, 14 Apr 2013 23:49:00 GMT
Accept-Ranges: bytes
Content-Length: 502365
Content-Type: text/plain
所以我的问题是第一种情况导致失败的原因是什么,我能做些什么。理想情况下,我会将文件留在原处。
*更新* *
我已经打印出 android 应用程序的标头,显然服务器没有为 android 代理发送 Content-Length。我不明白... :(
HTTP/1.1 200 OK
Accept-Ranges :
bytes
Cache-Control :
private,max-age=86400
Content-Disposition :
attachment; filename="04Left_Turning_Check_Step.m4v";
Content-Range :
bytes 0-7019776/7019777
Content-Type :
application/octet-stream;charset=UTF-8
Date :
Mon, 15 Apr 2013 08:39:06 GMT
Last-Modified :
Wed, 02 Jan 2013 18:40:09 GMT
Pragma :
cache
Server :
SSWS
Set-Cookie :
JSESSIONID=0B1D0CFFD975ED2736BFF7BBC23ACD96.V5; Path=/; HttpOnly
SS_MID=5a7bd305-4ac1-4d4d-98a3-67fa26c0db56hfje5d3d; Domain=.tangoberretin.com; Expires=Thu, 13-Apr-2023 08:39:06 GMT; Path=/
WebPersCookie=WIo8mRMTANAAke3GEsDOsONkGZoRcVvqpC4Am/pl/3WFB9hSEGH5DfGiDSKJDLc2nbZBpWFy3/Yfb3s=; path=/
Transfer-Encoding :
chunked
Vary :
Accept-Encoding
X-Android-Received-Millis :
1366015147201
X-Android-Sent-Millis :
1366015146842
X-ServedBy :
web115