2010-07-18 160 views
1

任何人都可以告訴我如何在從服務器下載文件之前獲取文件的大小(可能是http,ftp或其他)?如果您使用的是內置的HTTP Apahce的圖書館做了一個文件的請求,請幫助我..在android中下載文件之前獲取文件大小

問候 Varnesh

回答

2

,你可以只請求頭獲取文件大小。標題內將會有一個「內容長度」屬性,用於指示請求文件的字節數。請求標題不會請求文件本身。

2

試試這個會的情況下,工作的HTTP服務器是給文件大小

URL myUrl = new URL("http://jamants.com/file.mp3"); 
URLConnection urlConnection = myUrl.openConnection(); 
urlConnection.connect(); 
int file_size = urlConnection.getContentLength(); 

或第二版本試試這個。

URL myUrl = new URL("http://jamants.com/file.mp3"); 
myConnection = myUrl.openConnection(); 
List headersize = myConnection.getHeaderFields().get("content-Lenght"); 

它在我的項目中工作。

+0

如何從列表中讀取目標文件大小? – svp 2014-04-28 09:50:35