我在做多线程断点下载
[code="java"]//获得文件长度
URL url = new URL(downloadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode()==200){
this.fileSize = conn.getContentLength();//获取文件大小
}
Log.i(TAG, “fileSize =”+fileSize );[/code]
我发现不管downloadUrl是什么地址,比如下面2个:
http://cndns.newhua.com/down/Install_WLMessenger.zip
http://gx.newhua.com/down/FirefoxSetup3.6.13_cn.zip
下载到的都是15107字节,这是何故?Firefox和MSN都几十M呢!

为什么多线程断点下载到的文件都是15107字节?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- iteye_18182 2011-02-18 09:16关注
这样就可以了,加一句conn.setRequestProperty("referer", "http://www.newhua.com/Default.htm");
[code="java"]
public static void main(String[] args) throws Exception {
System.out.println(test("http://cndns.newhua.com/down/Install_WLMessenger.zip"));
System.out.println(test("http://gx.newhua.com/down/FirefoxSetup3.6.13_cn.zip"));
}static int test(String downloadUrl) throws Exception { URL url = new URL(downloadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); try{ conn.setRequestProperty("referer", "http://www.newhua.com/Default.htm"); if (conn.getResponseCode()==200){ System.out.println(conn.getHeaderFields()); return conn.getContentLength();//获取文件大小 } return -1; }finally{ conn.disconnect(); } };
[/code]
输出结果为:
[code="java"]
{Content-Length=[9816277], MicrosoftOfficeWebServer=[5.0_Pub], ETag=["2273d4b2f16acb1:71e"], Date=[Fri, 18 Feb 2011 01:15:12 GMT], Accept-Ranges=[bytes], Content-Type=[application/x-zip-compressed], Server=[Microsoft-IIS/6.0], Last-Modified=[Wed, 13 Oct 2010 16:14:23 GMT], null=[HTTP/1.1 200 OK], Content-Location=[http://cndns.newhua.com/down/Install_WLMessenger.zip]}
9816277
{Content-Length=[8405455], X-Powered-By=[ASP.NET], ETag=["4258a1211f98cb1:3a3"], Date=[Fri, 18 Feb 2011 01:15:24 GMT], Accept-Ranges=[bytes], Content-Type=[application/x-zip-compressed], Server=[Microsoft-IIS/6.0], Last-Modified=[Fri, 10 Dec 2010 04:02:58 GMT], null=[HTTP/1.1 200 OK]}
8405455[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报