我在做多线程断点下载
[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呢!
这样就可以了,加一句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]
你这两个下载文件,直接访问时,都跳转http://www.newhua.com/
[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{
if (conn.getResponseCode()==200){
System.out.println(conn.getHeaderFields());
return conn.getContentLength();//获取文件大小
}
return -1;
}finally{
conn.disconnect();
}
};
[/code]
输出
[code="java"]
{Content-Length=[15107], ETag=["1e2d754622c8cb1:3eb"], Date=[Thu, 17 Feb 2011 08:53:34 GMT], Accept-Ranges=[bytes], Content-Type=[text/html], Server=[Microsoft-IIS/6.0], Last-Modified=[Wed, 09 Feb 2011 06:26:24 GMT], null=[HTTP/1.1 200 OK], Content-Location=[http://www.newhua.com/Default.htm]}
15107
{Content-Length=[15107], ETag=["1e2d754622c8cb1:3eb"], Date=[Thu, 17 Feb 2011 08:53:34 GMT], Accept-Ranges=[bytes], Content-Type=[text/html], Server=[Microsoft-IIS/6.0], Last-Modified=[Wed, 09 Feb 2011 06:26:24 GMT], null=[HTTP/1.1 200 OK], Content-Location=[http://www.newhua.com/Default.htm]}
15107
[/code]