问题描述
我在做多线程断点下载//获得文件长度URL url = new URL(downloadUrl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();if (conn.getResponseCode()==200){ this.fileSize = conn.getContentLength();//获取文件大小}Log.i(TAG, “fileSize =”+fileSize );我发现不管downloadUrl是什么地址,比如下面2个:http://cndns.newhua.com/down/Install_WLMessenger.ziphttp://gx.newhua.com/down/FirefoxSetup3.6.13_cn.zip下载到的都是15107字节,这是何故?Firefox和MSN都几十M呢! 问题补充:vegas51225 写道
解决方案
这样就可以了,加一句conn.setRequestProperty("referer", "http://www.newhua.com/Default.htm");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(); } };输出结果为:{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
解决方案二:
你这两个下载文件,直接访问时,都跳转http://www.newhua.com/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(); } };输出{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