[code=java]
HttpGet httpGet=new HttpGet(downloadUrl);
HttpClient httpClient=new DefaultHttpClient();
HttpResponse httpResponse=httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity entity=httpResponse.getEntity();
fileSize=(int)entity.getContentLength();
}
[/code]
可以獲取文件的大小。
才能判斷服務器是否支持文件的分段下載嗎?
URLConnection conn = downloadUrl.openConnection();
conn.setAllowUserInteraction(true);
conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);
System.out.println(Thread.currentThread().getName() + " bytes="
+ startPos + "-" + endPos);
String messageString = conn.getHeaderField("Accept-Ranges");
if (!messageString.equals("bytes")) {
System.out.println("--------this server doesn't support ");
} else {
System.out.println("--------this server support");
}
注意一點,在用戶沒有設置conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);
System.out.println(Thread.currentThread().getName() + " bytes="
+ startPos + "-" + endPos);的時候,都會返回bytes;