开发者

getting the file size before downloading in android

Can anybody tell me how to get the size of a file before downloading it from server(may be http,ftp or anything) in android?.Does streaming works?. Please Help me..

Regards Varnes开发者_如何学编程h


Try this will work in case the http server is giving the file size

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

Or second Version try this.

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

It Works in my project.


If you are using the built-in Apahce HTTP library to make a request for a file, you can get the file size by requesting the headers only. Inside the headers will be a "content-length" attribute that will indicate the number of bytes of the requested file. Requesting the headers will not request the file itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜