原来网上代码一搜看到一段多点下载的代码,测试发现下载公司服务器上的文件的长度获取不到,源代码如下
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
//获得文件长度
long nEndPos =getFileSize(sURL);
打印响应头后发现Transfer-Encoding: chunked
在HTTP1.1协议中,RFC 2616中14.41章节中定义的Transfer-Encoding: chunked的头信息,chunked编码定义在3.6.1中,所有HTTP1.1 应用都支持此使用trunked编码动态的提供body内容的长度的方式。
后来又搜罗了一把,最后发现HttpClient开源项目能解决,并且android集成了这包
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet();
httpGet.setURI(new URI(urlStr));
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
fileSize = entity.getContentLength();
client.getConnectionManager().shutdown();
问题解决