Bahram
Bahram

Reputation: 1642

Android get content length return -1 in downloading file

I have this piece of code which is going to download some file ,

URL url = new URL("the url");
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();

but the getContentLength method returns -1 but the file is downloadable in any kind of internet browser .

Is this about MIME types of IIS or something ?

-------- ANSWER ------------- I've added a .map MIME Type ( same as zip files "application/x-zip-compressed") to IIS . That was solved the problem .

Upvotes: 0

Views: 709

Answers (4)

Bahram
Bahram

Reputation: 1642

I've added a .map MIME Type ( same as zip files "application/x-zip-compressed") to IIS . That was solved the problem .

Upvotes: 0

Lazy Ninja
Lazy Ninja

Reputation: 22527

it could be because the server is not setting content-length in the response header

Upvotes: 0

hardillb
hardillb

Reputation: 59618

Check the rest of the http headers, I would guess that the file is being streamed using chunked-encoding which means there will be no content length available.

I would suggest opening the inputstream for the connection and reading from it until it returns -1 for the end of the stream

Upvotes: 0

Russell Cargill
Russell Cargill

Reputation: 270

well it Returns the content length in bytes specified by the response header field content-length but it will return -1 if this field is not set.

Are you checking that you have definitely connected?

Upvotes: 1

Related Questions