Hossein Mobasher
Hossein Mobasher

Reputation: 4472

File extension type to be downloaded

How can I determine the file extension of a file I want to download? I am using the code below to download the file.

URL url = new URL("http://server.com/file.format");
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null));

InputStream in = client.execute(get).getEntity().getContent();

//... read input stream and save to file

I have had a look on other related threads, but I couldn't find a solution to me problem.

Upvotes: 0

Views: 1081

Answers (2)

MindSpiker
MindSpiker

Reputation: 1464

If I understand correctly your extension in your example is "format". In other words it is in the string used to make the URL used to request the file. To get the extension from a passed in file string used to make a URL use:

str.substring(str.lastIndexOf('.')+1)  

[Edited]

Upvotes: 1

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

On the internet, and in the unix world, there is no such thing as a mandatory file extension. That is a typical windows-world way of thinking.

In HTTP, the closest thing to a "file extension" is the mime type. It is an actual content type description such as text/html or image/gif. But HTTP does not supply a file extension. You'd have to map the mime types to the file extensions yourself!

Upvotes: 2

Related Questions